excel表格数据导入导出


/**
* 导出数据到excel表格
* Created by shenjianhua on 2018-12-28
*/
package com.luer.comm.excel;

import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.hssf.util.HSSFColor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* 导出数据到excel表格
* Created by shenjianhua on 2018-12-28
*/
@RestController
@RequestMapping("exportExcel")
public class ExportexcelUtil { /*
*
* 常用组件: HSSFWorkbook excel的文档对象 HSSFSheet excel的表单 HSSFRow excel的行 HSSFCell excel的格子单元 HSSFFont excel字体 样式: HSSFCellStyle cell样式
*
* */
public static void main(String [] args) throws Exception { //excel的表单名
String sheetName = "操作员数据表1";
//excel的标题名
String titleName = "操作员数据表2";
//提示 没啥用
String fileName = "操作员数据表3";
//int columnNumber = 3;
int columnNumber = 9;
//int[] columnWidth = { 10, 20, 30 };
//int[] columnWidth = {40,20,20,20,20,20,20,20,20};
String[][] dataList = { { "4e7a2bbe5cc24a63b214b3824596cd543","2018-12-27 10:34:19","2018-12-27 10:34:19","usernamedsfsdsf","passwrodsdlkjflsd","nicknamesdgfds","18896876323","1324325432.qq.com","remarkdsgfdg"},
{ "4e7a2bbe5cc24a63b214b38296cd543","2018-12-27 10:34:19","2018-12-27 10:34:19","usernamedsfsdsf","passwrodsdlkjflsd","nicknamesdgfds","18896876323","1324325432.qq.com","remarkdsgfdg" },
{ "9198a9f1acf74b648b50eee69d312276","2018-12-27 10:34:19","2018-12-27 10:34:19","usernamedsfsdsf","passwrodsdlkjflsd","nicknamesdgfds","18896876323","1324325432.qq.com","remarkdsgfdg"},
{ "9198a9f1acf14b648b50eee69d312276","2018-12-27 10:34:19","2018-12-27 10:34:19","usernamedsfsdsf","passwrodsdlkjflsd","nicknamesdgfds","18896876323","1324325432.qq.com","remarkdsgfdg"},
{ "9198a9f1acf24b648b50eee69d312276","2018-12-27 10:34:19","2018-12-27 10:34:19","usernamedsfsdsf","passwrodsdlkjflsd","nicknamesdgfds","18896876323","1324325432.qq.com","remarkdsgfdg"},
{ "9198a9f1acf34b648b50eee69d312276","2018-12-27 10:34:19","2018-12-27 10:34:19","usernamedsfsdsf","passwrodsdlkjflsd","nicknamesdgfds","18896876323","1324325432.qq.com","remarkdsgfdg"},
{ "9198a9f1acf44b648b50eee69d312276","2018-12-27 10:34:19","2018-12-27 10:34:19","usernamedsfsdsf","passwrodsdlkjflsd","nicknamesdgfds","18896876323","1324325432.qq.com","remarkdsgfdg"},
{ "9198a9f1acf54b648b50eee69d312276","2018-12-27 10:34:19","2018-12-27 10:34:19","usernamedsfsdsf","passwrodsdlkjflsd","nicknamesdgfds","18896876323","1324325432.qq.com","remarkdsgfdg"},
{ "9198a9f1acf64b648b50eee69d312276","2018-12-27 10:34:19","2018-12-27 10:34:19","usernamedsfsdsf","passwrodsdlkjflsd","nicknamesdgfds","18896876323","1324325432.qq.com","remarkdsgfdg"},
{ "9198a9f1acf84b648b50eee69d312276","2018-12-27 10:34:19","2018-12-27 10:34:19","usernamedsfsdsf","passwrodsdlkjflsd","nicknamesdgfds","18896876323","1324325432.qq.com","remarkdsgfdg"},
{ "9198a9f1acf94b648b50eee69d312276","2018-12-27 10:34:19","2018-12-27 10:34:19","usernamedsfsdsf","passwrodsdlkjflsd","nicknamesdgfds","18896876323","1324325432.qq.com","remarkdsgfdg"},
{ "9198a9f1acfw4b648b50eee69d312276","2018-12-27 10:34:19","2018-12-27 10:34:19","usernamedsfsdsf","passwrodsdlkjflsd","nicknamesdgfds","18896876323","1324325432.qq.com","remarkdsgfdg"}
};
String[] columnName = { "操作员id", "创造时间", "更新时间","登录账号","登录密码","昵称","手机号码","邮箱","注释" };
new ExportexcelUtil().ExportNoResponse(sheetName, titleName,
columnNumber, /*columnWidth,*/ columnName, dataList, fileName);
}
/*
* 导出数据到excel表格
* */ /*
* String titleName: excel标题名
*
* String sheetName: excel表单名
*
* int columnNumber: excel标题名的数量
*
* String[] columnName:excel标题名成
*
* String[][] dataList: 导入的数据集合
* */ @RequestMapping("/export")
public void ExportNoResponse( String titleName,String sheetName,
int columnNumber, /*int[] columnWidth,*/
String[] columnName, String[][] dataList,String fileName) throws Exception {
int flag = 0;
int[] columnWidth=new int[columnNumber];
for(flag=0;flag<columnNumber;flag++){
if(flag==0){
columnWidth[flag] = 40;
//System.out.println("columnWidth[i]: "+columnWidth[flag]);
}else{ columnWidth[flag] = 30;
//System.out.println("columnWidth[i]: "+columnWidth[flag]);
}
} if (columnNumber == columnWidth.length&& columnWidth.length == columnName.length) {
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet(sheetName);
// sheet.setDefaultColumnWidth(15); //统一设置列宽
for (int i = 0; i < columnNumber; i++)
{
for (int j = 0; j <= i; j++)
{
if (i == j)
{
sheet.setColumnWidth(i, columnWidth[j] * 256); // 单独设置每列的宽
}
}
}
// 创建第0行 也就是标题
HSSFRow row1 = sheet.createRow((int) 0);
row1.setHeightInPoints(50);// 设备标题的高度
// 第三步创建标题的单元格样式style2以及字体样式headerFont1
HSSFCellStyle style2 = wb.createCellStyle();
style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
style2.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index);
style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFFont headerFont1 = (HSSFFont) wb.createFont(); // 创建字体样式
headerFont1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗
headerFont1.setFontName("黑体"); // 设置字体类型
headerFont1.setFontHeightInPoints((short) 15); // 设置字体大小
style2.setFont(headerFont1); // 为标题样式设置字体样式 HSSFCell cell1 = row1.createCell(0);// 创建标题第一列
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0,
columnNumber - 1)); // 合并第0到第17列
cell1.setCellValue(titleName); // 设置值标题
cell1.setCellStyle(style2); // 设置标题样式 // 创建第1行 也就是表头
HSSFRow row = sheet.createRow((int) 1);
row.setHeightInPoints(37);// 设置表头高度 // 第四步,创建表头单元格样式 以及表头的字体样式
HSSFCellStyle style = wb.createCellStyle();
style.setWrapText(true);// 设置自动换行
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个居中格式 style.setBottomBorderColor(HSSFColor.BLACK.index);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN); HSSFFont headerFont = (HSSFFont) wb.createFont(); // 创建字体样式
headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗
headerFont.setFontName("黑体"); // 设置字体类型
headerFont.setFontHeightInPoints((short) 10); // 设置字体大小
style.setFont(headerFont); // 为标题样式设置字体样式 // 第四.一步,创建表头的列
for (int i = 0; i < columnNumber; i++)
{
HSSFCell cell = row.createCell(i);
cell.setCellValue(columnName[i]);
cell.setCellStyle(style);
} // 第五步,创建单元格,并设置值
for (int i = 0; i < dataList.length; i++)
{
row = sheet.createRow((int) i + 2);
// 为数据内容设置特点新单元格样式1 自动换行 上下居中
HSSFCellStyle zidonghuanhang = wb.createCellStyle();
zidonghuanhang.setWrapText(true);// 设置自动换行
zidonghuanhang
.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个居中格式 // 设置边框
zidonghuanhang.setBottomBorderColor(HSSFColor.BLACK.index);
zidonghuanhang.setBorderBottom(HSSFCellStyle.BORDER_THIN);
zidonghuanhang.setBorderLeft(HSSFCellStyle.BORDER_THIN);
zidonghuanhang.setBorderRight(HSSFCellStyle.BORDER_THIN);
zidonghuanhang.setBorderTop(HSSFCellStyle.BORDER_THIN); // 为数据内容设置特点新单元格样式2 自动换行 上下居中左右也居中
HSSFCellStyle zidonghuanhang2 = wb.createCellStyle();
zidonghuanhang2.setWrapText(true);// 设置自动换行
zidonghuanhang2
.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个上下居中格式
zidonghuanhang2.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中 // 设置边框
zidonghuanhang2.setBottomBorderColor(HSSFColor.BLACK.index);
zidonghuanhang2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
zidonghuanhang2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
zidonghuanhang2.setBorderRight(HSSFCellStyle.BORDER_THIN);
zidonghuanhang2.setBorderTop(HSSFCellStyle.BORDER_THIN);
HSSFCell datacell = null;
for (int j = 0; j < columnNumber; j++)
{
datacell = row.createCell(j);
datacell.setCellValue(dataList[i][j]);
datacell.setCellStyle(zidonghuanhang2);
}
} // 第六步,将文件存到指定位置
try {
FileOutputStream fout = new FileOutputStream("D:operator.xls");
wb.write(fout);
String str = "导出" + fileName + "成功!";
System.out.println(str);
fout.close();
} catch (Exception e) {
e.printStackTrace();
String str1 = "导出" + fileName + "失败!";
System.out.println(str1);
}
} else {
System.out.println("列数目长度名称三个数组长度要一致");
} } }
/**
* Excel 导入到mysql
* Created by shenjianhua on 2018-12-28
*/
package com.luer.comm.excel;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List; import com.luer.operator.bean.Operator;
import com.luer.operator.service.OperatorService;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; /**
* Excel 导入到mysql
* Created by shenjianhua on 2019-1-3
*/
@RestController
@RequestMapping("/importExcel")
public class ImportexcelUtil { private static Logger logger = Logger.getLogger(ImportexcelUtil.class);
private final static String xls = "xls";
private final static String xlsx = "xlsx"; /**
* 读入excel文件,解析后返回
*
* @param
* @throws IOException
*/ @Autowired
OperatorService operatorService; /*
* 读取excel数据导入数据库
* */
@ResponseBody
@RequestMapping("/readExcel")
public List<String[]> readExcel(MultipartFile file) throws Exception {
//检查文件
checkFile(file);
//获得Workbook工作薄对象
Workbook workbook = getWorkBook(file);
//创建返回对象,把每行中的值作为一个数组,所有行作为一个集合返回
List<String[]> list = new ArrayList<String[]>();
if (workbook != null) {
for (int sheetNum = 0; sheetNum < workbook.getNumberOfSheets(); sheetNum++) {
//获得当前sheet工作表
Sheet sheet = workbook.getSheetAt(sheetNum);
if (sheet == null) {
continue;
}
//获得当前sheet的开始行
int firstRowNum = sheet.getFirstRowNum();
//获得当前sheet的结束行
int lastRowNum = sheet.getLastRowNum();
//循环除了第一行的所有行
for (int rowNum = firstRowNum + 1; rowNum <= lastRowNum; rowNum++) {
//获得当前行
Row row = sheet.getRow(rowNum);
if (row == null) {
continue;
}
//获得当前行的开始列
int firstCellNum = row.getFirstCellNum();
//获得当前行的列数
int lastCellNum = row.getPhysicalNumberOfCells();
String[] cells = new String[row.getPhysicalNumberOfCells()];
//循环当前行
for (int cellNum = firstCellNum; cellNum < lastCellNum; cellNum++) {
Cell cell = row.getCell(cellNum);
cells[cellNum] = getCellValue(cell);
}
list.add(cells);
} }
//workbook.close();
} /*
* 封装对象存入数据库
*
* 修改字段的遍历次数
* */
for(int k=1;k<9;k++) {
Operator operator = new Operator();
if(null != list.get(k)[0]){
operator.setId(list.get(k)[0]);
}
if(null != list.get(k)[1]){
operator.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(list.get(k)[1]));
/* String string = "2016-10-24 21:59:06";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.parse(list.get(k)[1]));*/
}
if(null != list.get(k)[2]){
operator.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(list.get(k)[1]));
}
if(null != list.get(k)[3]){
operator.setUsername(list.get(3)[3]);
}
if(null != list.get(k)[4]){
operator.setPassword(list.get(k)[4]);
}
if(null != list.get(k)[5]){
operator.setNickname(list.get(k)[5]);
}
if(null != list.get(k)[6]){
operator.setPhoneNumber(list.get(k)[6]);
}
if(null != list.get(k)[7]){
operator.setEmail(list.get(k)[8]);
}
if(null != list.get(k)[8]){
operator.setRemark(list.get(k)[8]);
}
operatorService.insertOperator(operator);
} return list;
} @ResponseBody
@RequestMapping("/readExcel2")
public String[] readExcel2(MultipartFile file) throws IOException {
//检查文件
checkFile(file);
//获得Workbook工作薄对象
Workbook workbook = getWorkBook(file);
//创建返回对象,把每行中的值作为一个数组,所有行作为一个集合返回
List<String[]> list = new ArrayList<String[]>();
if (workbook != null) {
for (int sheetNum = 0; sheetNum < workbook.getNumberOfSheets(); sheetNum++) {
//获得当前sheet工作表
Sheet sheet = workbook.getSheetAt(sheetNum);
if (sheet == null) {
continue;
}
//获得当前sheet的开始行
int firstRowNum = sheet.getFirstRowNum();
//获得当前sheet的结束行
int lastRowNum = sheet.getLastRowNum();
//循环除了第一行的所有行
for (int rowNum = firstRowNum + 1; rowNum <= lastRowNum; rowNum++) {
//获得当前行
Row row = sheet.getRow(rowNum);
if (row == null) {
continue;
}
//获得当前行的开始列
int firstCellNum = row.getFirstCellNum();
//获得当前行的列数
int lastCellNum = row.getPhysicalNumberOfCells();
String[] cells = new String[row.getPhysicalNumberOfCells()];
//循环当前行
for (int cellNum = firstCellNum; cellNum < lastCellNum; cellNum++) {
Cell cell = row.getCell(cellNum);
cells[cellNum] = getCellValue(cell);
}
list.add(cells);
}
}
// workbook.close();
} return list.get(0);
} public static void checkFile(MultipartFile file) throws IOException {
//判断文件是否存在
if (null == file) {
logger.error("文件不存在!");
throw new FileNotFoundException("文件不存在!");
}
//获得文件名
String fileName = file.getOriginalFilename();
//判断文件是否是excel文件
if (!fileName.endsWith(xls) && !fileName.endsWith(xlsx)) {
logger.error(fileName + "不是excel文件");
throw new IOException(fileName + "不是excel文件");
}
} public static Workbook getWorkBook(MultipartFile file) {
//获得文件名
String fileName = file.getOriginalFilename();
//创建Workbook工作薄对象,表示整个excel
Workbook workbook = null;
try {
//获取excel文件的io流
InputStream is = file.getInputStream();
//根据文件后缀名不同(xls和xlsx)获得不同的Workbook实现类对象
if (fileName.endsWith(xls)) {
//2003
workbook = new HSSFWorkbook(is);
} else if (fileName.endsWith(xlsx)) {
//2007 及2007以上
workbook = new XSSFWorkbook(is);
}
} catch (IOException e) {
logger.info(e.getMessage());
}
return workbook;
} public static String getCellValue(Cell cell) {
String cellValue = "";
if (cell == null) {
return cellValue;
}
//把数字当成String来读,避免出现1读成1.0的情况
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
cell.setCellType(Cell.CELL_TYPE_STRING);
}
//判断数据的类型
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC: //数字
cellValue = String.valueOf(cell.getNumericCellValue());
break;
case Cell.CELL_TYPE_STRING: //字符串
cellValue = String.valueOf(cell.getStringCellValue());
break;
case Cell.CELL_TYPE_BOOLEAN: //Boolean
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA: //公式
cellValue = String.valueOf(cell.getCellFormula());
break;
case Cell.CELL_TYPE_BLANK: //空值
cellValue = "";
break;
case Cell.CELL_TYPE_ERROR: //故障
cellValue = "非法字符";
break;
default:
cellValue = "未知类型";
break;
}
return cellValue;
}
} 如果帮助到你 给点鼓励 点个赞吧 谢谢亲
上一篇:Spring Batch 批处理框架


下一篇:3.高并发教程-基础篇-之分布式全文搜索引擎elasticsearch的搭建