Commit 78eaf16a authored by shulidong's avatar shulidong

委托单产值导出表头生成

parent 6c727004
...@@ -2,22 +2,30 @@ package cn.wise.sc.cement.business.util; ...@@ -2,22 +2,30 @@ package cn.wise.sc.cement.business.util;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import cn.afterturn.easypoi.excel.annotation.Excel; import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.hutool.core.util.ArrayUtil;
import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString; import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.wp.usermodel.Paragraph; import org.apache.poi.wp.usermodel.Paragraph;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont; import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRichTextString; import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
...@@ -28,300 +36,408 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook; ...@@ -28,300 +36,408 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
*/ */
public class ExcelUtil { public class ExcelUtil {
public static void excelLocal(String path, String fileName, String[] headers, List<Object[]> datas) { public static void excelLocal(String path, String fileName, String[] headers, List<Object[]> datas) {
Workbook workbook = getWorkbook(headers, datas); Workbook workbook = getWorkbook(headers, datas);
if (workbook != null) { if (workbook != null) {
ByteArrayOutputStream byteArrayOutputStream = null; ByteArrayOutputStream byteArrayOutputStream = null;
FileOutputStream fileOutputStream = null; FileOutputStream fileOutputStream = null;
try { try {
byteArrayOutputStream = new ByteArrayOutputStream(); byteArrayOutputStream = new ByteArrayOutputStream();
workbook.write(byteArrayOutputStream); workbook.write(byteArrayOutputStream);
String suffix = ".xls"; String suffix = ".xls";
File file = new File(path + File.separator + fileName + suffix); File file = new File(path + File.separator + fileName + suffix);
if (!file.getParentFile().exists()) { if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs(); file.getParentFile().mkdirs();
} }
fileOutputStream = new FileOutputStream(file); fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(byteArrayOutputStream.toByteArray()); fileOutputStream.write(byteArrayOutputStream.toByteArray());
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
try { try {
if (fileOutputStream != null) { if (fileOutputStream != null) {
fileOutputStream.close(); fileOutputStream.close();
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
try { try {
if (byteArrayOutputStream != null) { if (byteArrayOutputStream != null) {
byteArrayOutputStream.close(); byteArrayOutputStream.close();
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
try { try {
workbook.close(); workbook.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
} }
/** /**
* 导出excel * 导出excel
* *
* @param fileName * @param fileName
* @param headers * @param headers
* @param datas * @param datas
* @param response * @param response
*/ */
public static void excelExport(String fileName, String[] headers, List<Object[]> datas, public static void excelExport(String fileName, String[] headers, List<Object[]> datas,
HttpServletResponse response) { HttpServletResponse response) {
Workbook workbook = getWorkbook(headers, datas); Workbook workbook = getWorkbook(headers, datas);
if (workbook != null) { if (workbook != null) {
ByteArrayOutputStream byteArrayOutputStream = null; ByteArrayOutputStream byteArrayOutputStream = null;
try { try {
byteArrayOutputStream = new ByteArrayOutputStream(); byteArrayOutputStream = new ByteArrayOutputStream();
workbook.write(byteArrayOutputStream); workbook.write(byteArrayOutputStream);
String suffix = ".xls"; String suffix = ".xls";
response.setContentType("application/vnd.ms-excel;charset=utf-8"); response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition", response.setHeader("Content-Disposition",
"attachment;filename=" + new String((fileName + suffix).getBytes(), "iso-8859-1")); "attachment;filename=" + new String((fileName + suffix).getBytes(), "iso-8859-1"));
OutputStream outputStream = response.getOutputStream(); OutputStream outputStream = response.getOutputStream();
outputStream.write(byteArrayOutputStream.toByteArray()); outputStream.write(byteArrayOutputStream.toByteArray());
outputStream.close(); outputStream.close();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
try { try {
if (byteArrayOutputStream != null) { if (byteArrayOutputStream != null) {
byteArrayOutputStream.close(); byteArrayOutputStream.close();
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
try { try {
workbook.close(); workbook.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
} }
public static void excelExportNew(String fileName, String[] headers, List<Object[]> datas,
HttpServletResponse response) { public static void excelExportNew(String fileName, String[] headers, List<Object[]> datas,
Workbook workbook = getWorkbookNew(headers, datas); HttpServletResponse response) {
if (workbook != null) { Workbook workbook = getWorkbookNew(headers, datas);
ByteArrayOutputStream byteArrayOutputStream = null; if (workbook != null) {
try { ByteArrayOutputStream byteArrayOutputStream = null;
byteArrayOutputStream = new ByteArrayOutputStream(); try {
workbook.write(byteArrayOutputStream); byteArrayOutputStream = new ByteArrayOutputStream();
workbook.write(byteArrayOutputStream);
String suffix = ".xls";
response.setContentType("application/vnd.ms-excel;charset=utf-8"); String suffix = ".xls";
response.setHeader("Content-Disposition", response.setContentType("application/vnd.ms-excel;charset=utf-8");
"attachment;filename=" + new String((fileName + suffix).getBytes(), "iso-8859-1")); response.setHeader("Content-Disposition",
"attachment;filename=" + new String((fileName + suffix).getBytes(), "iso-8859-1"));
OutputStream outputStream = response.getOutputStream();
outputStream.write(byteArrayOutputStream.toByteArray()); OutputStream outputStream = response.getOutputStream();
outputStream.close(); outputStream.write(byteArrayOutputStream.toByteArray());
} catch (Exception e) { outputStream.close();
e.printStackTrace(); } catch (Exception e) {
} finally { e.printStackTrace();
try { } finally {
if (byteArrayOutputStream != null) { try {
byteArrayOutputStream.close(); if (byteArrayOutputStream != null) {
} byteArrayOutputStream.close();
} catch (IOException e) { }
e.printStackTrace(); } catch (IOException e) {
} e.printStackTrace();
}
try {
workbook.close(); try {
} catch (IOException e) { workbook.close();
e.printStackTrace(); } catch (IOException e) {
} e.printStackTrace();
} }
} }
} }
/** }
* @param headers 列头
* @param datas 数据 /**
* @return * @param headers 列头
*/ * @param datas 数据
public static Workbook getWorkbook(String[] headers, List<Object[]> datas) { * @return
XSSFWorkbook workbook = new XSSFWorkbook(); */
XSSFSheet sheet = workbook.createSheet(); public static Workbook getWorkbook(String[] headers, List<Object[]> datas) {
XSSFWorkbook workbook = new XSSFWorkbook();
Row row = null; XSSFSheet sheet = workbook.createSheet();
Cell cell = null;
XSSFFont font = workbook.createFont(); Row row = null;
int line = 0, maxColumn = 0; Cell cell = null;
if (headers != null && headers.length > 0) { // 设置列头 XSSFFont font = workbook.createFont();
CellStyle style = workbook.createCellStyle(); int line = 0, maxColumn = 0;
style.setAlignment(HorizontalAlignment.CENTER_SELECTION); if (headers != null && headers.length > 0) { // 设置列头
CellStyle style = workbook.createCellStyle();
row = sheet.createRow(line++); style.setAlignment(HorizontalAlignment.CENTER_SELECTION);
row.setHeightInPoints(23);
font.setBold(true); row = sheet.createRow(line++);
font.setFontHeightInPoints((short) 13); row.setHeightInPoints(23);
style.setFont(font); font.setBold(true);
font.setFontHeightInPoints((short) 13);
maxColumn = headers.length; style.setFont(font);
for (int i = 0; i < maxColumn; i++) {
cell = row.createCell(i); maxColumn = headers.length;
cell.setCellValue(headers[i]); for (int i = 0; i < maxColumn; i++) {
cell.setCellStyle(style); cell = row.createCell(i);
} cell.setCellValue(headers[i]);
} cell.setCellStyle(style);
}
if (datas != null && datas.size() > 0) {// 渲染数据 }
CellStyle style = workbook.createCellStyle();
XSSFFont xxsfFont = workbook.createFont(); if (datas != null && datas.size() > 0) {// 渲染数据
CellStyle style = workbook.createCellStyle();
for (int index = 0, size = datas.size(); index < size; index++) { XSSFFont xxsfFont = workbook.createFont();
Object[] data = datas.get(index);
if (data != null && data.length > 0) { for (int index = 0, size = datas.size(); index < size; index++) {
row = sheet.createRow(line++); Object[] data = datas.get(index);
row.setHeightInPoints(20); if (data != null && data.length > 0) {
row = sheet.createRow(line++);
int length = data.length; row.setHeightInPoints(20);
if (length > maxColumn) {
maxColumn = length; int length = data.length;
} if (length > maxColumn) {
maxColumn = length;
for (int i = 0; i < length; i++) { }
String value = data[i] == null ? null : data[i].toString();
cell = row.createCell(i); for (int i = 0; i < length; i++) {
//处理下标 String value = data[i] == null ? null : data[i].toString();
if(value.contains("<sub>") || value.contains("</sub>") ){ cell = row.createCell(i);
xxsfFont.setTypeOffset(FontFormatting.SS_SUB); //处理下标
cell.setCellStyle(style); if (value.contains("<sub>") || value.contains("</sub>")) {
XSSFRichTextString richString = new XSSFRichTextString(value.replaceAll("</sub>","<sub>").replaceAll("<sub>","")); xxsfFont.setTypeOffset(FontFormatting.SS_SUB);
//提取下标位置 cell.setCellStyle(style);
applyRichStringFontsub(value, richString, xxsfFont); XSSFRichTextString richString = new XSSFRichTextString(value.replaceAll("</sub>", "<sub>").replaceAll("<sub>", ""));
cell.setCellValue(richString); //提取下标位置
}else if(value.contains("<sup>") || value.contains("</sup>")){ applyRichStringFontsub(value, richString, xxsfFont);
xxsfFont.setTypeOffset(FontFormatting.SS_SUPER); cell.setCellValue(richString);
cell.setCellStyle(style); } else if (value.contains("<sup>") || value.contains("</sup>")) {
XSSFRichTextString richString = new XSSFRichTextString(value.replaceAll("</sup>","<sup>").replaceAll("<sup>","")); xxsfFont.setTypeOffset(FontFormatting.SS_SUPER);
//提取下标位置 cell.setCellStyle(style);
applyRichStringFontsup(value, richString, xxsfFont); XSSFRichTextString richString = new XSSFRichTextString(value.replaceAll("</sup>", "<sup>").replaceAll("<sup>", ""));
cell.setCellValue(richString); //提取下标位置
}else{ applyRichStringFontsup(value, richString, xxsfFont);
cell.setCellValue(value); cell.setCellValue(richString);
} else {
cell.setCellStyle(style); cell.setCellValue(value);
}
cell.setCellStyle(style);
} }
}
} }
} }
}
for (int i = 0; i < maxColumn; i++) { }
sheet.autoSizeColumn(i);
} for (int i = 0; i < maxColumn; i++) {
sheet.autoSizeColumn(i);
return workbook; }
}
return workbook;
private static void applyRichStringFontsub(String sub, XSSFRichTextString richString,XSSFFont xxsfFont) { }
String[] split = sub.replaceAll("</sub>", "<sub>").split("<sub>");
String it_str = ""; /**
for (int i = 1; i < split.length + 1; i++) { * @param infoHeader
if (i % 2 != 0) { * @param teamsHeader
it_str = it_str + split[i - 1]; * @return
} else { */
richString.applyFont(it_str.length(),(it_str+split[i-1]).length(),xxsfFont); private static XSSFWorkbook generatoronsignationWorkbook(List<String> infoHeader, List<String> teamsHeader) {
it_str = it_str + split[i - 1]; XSSFWorkbook wb = new XSSFWorkbook();
} XSSFSheet sheet = wb.createSheet("sheet1");
XSSFCellStyle style=wb.createCellStyle();
} //水平居中
} style.setAlignment(HorizontalAlignment.CENTER);
//垂直居中
private static void applyRichStringFontsup(String sup, XSSFRichTextString richString,XSSFFont xxsfFont) { style.setVerticalAlignment(VerticalAlignment.CENTER);
String[] split = sup.replaceAll("</sup>", "<sup>").split("<sup>");
String it_str = ""; int rownum = 0;
for (int i = 1; i < split.length + 1; i++) { //创建第一行
if (i % 2 != 0) { XSSFRow row0 = sheet.createRow(rownum++);
it_str = it_str + split[i - 1]; //处理委托人信息表头
} else { for (int i = 0; i < infoHeader.size(); i++) {
richString.applyFont(it_str.length(),(it_str+split[i-1]).length(),xxsfFont); XSSFCell tempCell = row0.createCell(i);
it_str = it_str + split[i - 1]; tempCell.setCellValue(infoHeader.get(i));
} tempCell.setCellStyle(style);
}
} //处理检测项总产值表头
} for (int i = infoHeader.size(); i < teamsHeader.size() + infoHeader.size(); i++) {
XSSFCell tempCell = row0.createCell(i);
tempCell.setCellValue(teamsHeader.get(i - infoHeader.size()));
tempCell.setCellStyle(style);
public static Workbook getWorkbookNew(String[] headers, List<Object[]> datas) { }
Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet(); //增加1空列
workbook.setSheetName(0,"样品处置记录"); {
XSSFCell tempCell = row0.createCell(teamsHeader.size() + infoHeader.size());
Row row = null; tempCell.setCellValue("");
Cell cell = null; tempCell.setCellStyle(style);
}
Font font = workbook.createFont(); //处理检测项信息表头
for (int i = teamsHeader.size() + infoHeader.size() + 1; i < infoHeader.size() + 1 + teamsHeader.size() * 3; i++) {
int line = 0, maxColumn = 0; String header = teamsHeader.get((i - teamsHeader.size() - infoHeader.size() - 1) / 2);
if (headers != null && headers.length > 0) { // 设置列头 XSSFCell tempCell1 = row0.createCell(i);
CellStyle style = workbook.createCellStyle(); tempCell1.setCellStyle(style);
style.setAlignment(HorizontalAlignment.CENTER_SELECTION); tempCell1.setCellValue(header);
style.setVerticalAlignment(VerticalAlignment.CENTER); XSSFCell tempCell2 = row0.createCell(++i);
row = sheet.createRow(line++); tempCell2.setCellValue(header);
row.setHeightInPoints(23); tempCell2.setCellStyle(style);
font.setFontHeightInPoints((short) 12); //合并表头
font.setFontName("宋体"); sheet.addMergedRegion(new CellRangeAddress(0, 0, i-1, i ));
style.setFont(font); }
//创建第二行
maxColumn = headers.length; XSSFRow row1 = sheet.createRow(rownum++);
for (int i = 0; i < maxColumn; i++) { //处理委托人信息表头
cell = row.createCell(i); for (int i = 0; i < infoHeader.size(); i++) {
cell.setCellValue(headers[i]); XSSFCell tempCell = row1.createCell(i);
cell.setCellStyle(style); tempCell.setCellValue("");
} tempCell.setCellStyle(style);
} //合并表头
sheet.addMergedRegion(new CellRangeAddress(0, 1, i, i));
if (datas != null && datas.size() > 0) {// 渲染数据 }
CellStyle style = workbook.createCellStyle(); //处理检测项总产值表头
for (int index = 0, size = datas.size(); index < size; index++) { for (int i = infoHeader.size(); i < teamsHeader.size() + infoHeader.size(); i++) {
Object[] data = datas.get(index); XSSFCell tempCell = row1.createCell(i);
if (data != null && data.length > 0) { tempCell.setCellValue("计算产值");
row = sheet.createRow(line++); tempCell.setCellStyle(style);
row.setHeightInPoints(20); }
//增加1空列
int length = data.length; {
if (length > maxColumn) { XSSFCell tempCell = row0.createCell(teamsHeader.size() + infoHeader.size());
maxColumn = length; tempCell.setCellValue("");
} tempCell.setCellStyle(style);
}
for (int i = 0; i < length; i++) { //处理检测项信息表头
style.setWrapText(true); //关键 for (int i = teamsHeader.size()+1 + infoHeader.size(); i < infoHeader.size()+1 + teamsHeader.size() * 3; i++) {
style.setFont(font); XSSFCell tempCell1 = row1.createCell(i);
cell = row.createCell(i); tempCell1.setCellValue("单价");
cell.setCellValue(data[i] == null ? null : data[i].toString()); tempCell1.setCellStyle(style);
cell.setCellStyle(style); XSSFCell tempCell2 = row1.createCell(++i);
style.setAlignment(HorizontalAlignment.CENTER_SELECTION); tempCell2.setCellValue("数量");
} tempCell2.setCellStyle(style);
} }
}
} return wb;
}
for (int i = 0; i < maxColumn; i++) {
sheet.autoSizeColumn(i); public static void main(String[] args) throws IOException {
} //指定数据存放的位置
OutputStream outputStream = new FileOutputStream("D:\\test.xlsx");
return workbook; String[] in_fo = {
} "委托单号",
"委托单位",
"项目编号",
"项目名称",
"委托人",
"样品数量",
"发样日期",
"是否加急",
"实际产值",
};
List info = Arrays.asList(in_fo);
String a = "荧光检测 L.O.I SiO2 Al2O3+TiO2 Al2O3 TiO2 Fe2O3 CaO MgO K2O Na2O 有效钾 有效钠 重量法SO3 SO3 挥发性硫 硫酸盐硫 Cl⁻ fCaO 1350 fCaO 1400 fCaO 1450 fCaO 附着水 结晶水 工业分析 St,ad 水分 密度 比表面积 45μm细度 80μm细度 200μm细度 激光粒度分析 颗粒形貌分析 塑性指数 白度 粒度筛析 易磨性 T3000 T5000 磨蚀性 可磨 辊磨 易烧性 其他-工艺性能 MnO TiO2 五氧化二磷 铜(Cu) 铅(Pb) 锰(Mn) 镍(Ni) 锌(Zn) 钴(Co) 总铬(Cr) 镉(Cd) 汞(Hg) 砷(As) 锑(Sb) 水溶性铬(VI) 水泥强度检测1天 水泥强度检测2天 水泥强度检测3天 水泥强度检测7天 水泥强度检测28天 标准稠度用水量 流动度 凝结时间 安定性 岩相 衍射 弹筒发热量 热分析 金属铁 水化热 燃烧特性 不溶物 氟离子 游离硅 f-SiO2 f-SiO2+45 f-SiO2+80 其他 煤中硫";
String[] te_rm = a.split("\t");
List term = Arrays.asList(te_rm);
XSSFWorkbook workbook = generatoronsignationWorkbook(info, term);
workbook.write(outputStream);
outputStream.close();
}
private static void applyRichStringFontsub(String sub, XSSFRichTextString richString, XSSFFont xxsfFont) {
String[] split = sub.replaceAll("</sub>", "<sub>").split("<sub>");
String it_str = "";
for (int i = 1; i < split.length + 1; i++) {
if (i % 2 != 0) {
it_str = it_str + split[i - 1];
} else {
richString.applyFont(it_str.length(), (it_str + split[i - 1]).length(), xxsfFont);
it_str = it_str + split[i - 1];
}
}
}
private static void applyRichStringFontsup(String sup, XSSFRichTextString richString, XSSFFont xxsfFont) {
String[] split = sup.replaceAll("</sup>", "<sup>").split("<sup>");
String it_str = "";
for (int i = 1; i < split.length + 1; i++) {
if (i % 2 != 0) {
it_str = it_str + split[i - 1];
} else {
richString.applyFont(it_str.length(), (it_str + split[i - 1]).length(), xxsfFont);
it_str = it_str + split[i - 1];
}
}
}
public static Workbook getWorkbookNew(String[] headers, List<Object[]> datas) {
Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet();
workbook.setSheetName(0, "样品处置记录");
Row row = null;
Cell cell = null;
Font font = workbook.createFont();
int line = 0, maxColumn = 0;
if (headers != null && headers.length > 0) { // 设置列头
CellStyle style = workbook.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER_SELECTION);
style.setVerticalAlignment(VerticalAlignment.CENTER);
row = sheet.createRow(line++);
row.setHeightInPoints(23);
font.setFontHeightInPoints((short) 12);
font.setFontName("宋体");
style.setFont(font);
maxColumn = headers.length;
for (int i = 0; i < maxColumn; i++) {
cell = row.createCell(i);
cell.setCellValue(headers[i]);
cell.setCellStyle(style);
}
}
if (datas != null && datas.size() > 0) {// 渲染数据
CellStyle style = workbook.createCellStyle();
for (int index = 0, size = datas.size(); index < size; index++) {
Object[] data = datas.get(index);
if (data != null && data.length > 0) {
row = sheet.createRow(line++);
row.setHeightInPoints(20);
int length = data.length;
if (length > maxColumn) {
maxColumn = length;
}
for (int i = 0; i < length; i++) {
style.setWrapText(true); //关键
style.setFont(font);
cell = row.createCell(i);
cell.setCellValue(data[i] == null ? null : data[i].toString());
cell.setCellStyle(style);
style.setAlignment(HorizontalAlignment.CENTER_SELECTION);
}
}
}
}
for (int i = 0; i < maxColumn; i++) {
sheet.autoSizeColumn(i);
}
return workbook;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment