package com.product.org.admin.util;
|
|
import java.io.IOException;
|
import java.io.OutputStream;
|
import java.net.URLEncoder;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
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 com.product.core.entity.DataTableEntity;
|
import com.product.core.entity.FieldSetEntity;
|
import com.product.core.exception.BaseException;
|
|
/**
|
* 写入EXCEL数据
|
* Copyright LX-BASE
|
* @Title: LX-BASE-
|
* @Project: WriteInExcelUtil
|
* @Date: 2020-10-09 11:00
|
* @Author: 杜洪波
|
* @Description:
|
*/
|
public class WriteInExcelUtil {
|
|
public static void testWrite(HttpServletResponse response,FieldSetEntity fse) throws IOException, BaseException{
|
|
String[] error=getErrorInfo(fse);
|
|
|
String fileName = "企业信息.xls";
|
response.setContentType("application/octet-stream");
|
response.setHeader("name", fileName);
|
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
|
response.setHeader("Pragma", "public");
|
response.setDateHeader("Expires", 0);
|
response.setHeader("Content-disposition","attachment; filename=\"" + URLEncoder.encode(fileName, "UTF-8") + "\"");
|
|
//创建工作簿
|
Workbook wb = new HSSFWorkbook();
|
//创建 Sheet页
|
Sheet sheetA = wb.createSheet("error");
|
|
//生成行数
|
for (int i = 0; i <= error.length; i++) {
|
|
//创建行
|
Row row = sheetA.createRow(i);
|
//第一行数据(EXCEL位置, 错误数据, 错误信息)
|
if (i==0) {
|
for (int j = 0; j < 3; j++) {
|
Cell cell = row.createCell(j);
|
if (j==0) {
|
cell.setCellValue("position");
|
}else if(j==1) {
|
cell.setCellValue("errorData");
|
}else {
|
cell.setCellValue("errorInfo");
|
}
|
}
|
}else { //其他行错误信息
|
String []errors=error[i-1].split("<error>");
|
|
for (int j = 0; j < 3; j++) {
|
Cell cell = row.createCell(j);
|
if (j==0) {
|
cell.setCellValue(errors[0]);
|
}else if(j==1){
|
cell.setCellValue(errors[1]);
|
} else {
|
cell.setCellValue(errors[2]);
|
}
|
}
|
}
|
}
|
|
//路径需要存在
|
OutputStream fos = response.getOutputStream();
|
wb.write(fos);
|
fos.close();
|
wb.close();
|
}
|
|
public static String[] getErrorInfo(FieldSetEntity fse) throws BaseException {
|
|
Object [] fields=fse.getFields();
|
String [] error=new String[fields.length];
|
|
for (int i = 0; i < fields.length; i++) {
|
String field=fields[i].toString();
|
error[i]=fse.getString(field);
|
}
|
|
return error;
|
}
|
}
|