package com.aisino.cxtj.utils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
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.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.Region;
/**
* Excel工具类
* @ClassName: ExcelUtil
* @author CXJ
* @date 2016年8月3日
*/
public class TjExcelUtil {
private static HSSFWorkbook wb;
private static CellStyle titleStyle; // 标题行样式
private static Font titleFont; // 标题行字体
private static CellStyle dateStyle; // 日期行样式
private static Font dateFont; // 日期行字体
private static CellStyle headStyle; // 表头行样式
private static Font headFont; // 表头行字体
private static CellStyle contentStyle; // 内容行样式
private static Font contentFont; // 内容行字体
/**
* 导出文件
*
* @param setInfo
* @param outputExcelFileName
* @return
* @throws IOException
*/
public static boolean export2File(ExcelExportData setInfo,
String outputExcelFileName,String tjlx) throws Exception {
return FileUtil.write(outputExcelFileName, export2ByteArray(setInfo,tjlx),true, true);
}
/**
* 导出到byte数组
*
* @param setInfo
* @return
* @throws Exception
*/
public static byte[] export2ByteArray(ExcelExportData setInfo, String tjlx)
throws Exception {
return export2Stream(setInfo,tjlx).toByteArray();
}
/**
* 导出到流
*
* @param setInfo
* @return
* @throws Exception
*/
public static ByteArrayOutputStream export2Stream(ExcelExportData setInfo, String tjlx)
throws Exception {
init();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Set<Entry<String, List<?>>> set = setInfo.getDataMap().entrySet();
String[] sheetNames = new String[setInfo.getDataMap().size()];
int sheetNameNum = 0;
for (Entry<String, List<?>> entry : set) {
sheetNames[sheetNameNum] = entry.getKey();
sheetNameNum++;
}
HSSFSheet[] sheets = getSheets(setInfo.getDataMap().size(), sheetNames);
int sheetNum = 0;
for (Entry<String, List<?>> entry : set) {
// Sheet
List<?> objs = entry.getValue();
// 标题行
createTableTitleRow(setInfo, sheets, sheetNum);
// 日期行 (导出日期)
createTableDateRow(setInfo, sheets, sheetNum ,tjlx);
// 表头
//creatTableHeadRow(setInfo, sheets, sheetNum);
// 表体
String[] fieldNames = setInfo.getFieldNames().get(sheetNum);
int rowNum = 4;
for (Object obj : objs) {
HSSFRow contentRow = sheets[sheetNum].createRow(rowNum);
contentRow.setHeight((short) 300);
HSSFCell[] cells = getCells(contentRow, setInfo.getFieldNames().get(sheetNum).length);
int cellNum = 1; // 去掉一列序号,因此从1开始
if (fieldNames != null) {
for (int num = 0; num < fieldNames.length; num++) {
Object value = ReflectionUtil.invokeGetterMethod(obj,fieldNames[num]);
cells[cellNum].setCellValue(value == null ? "" : value.toString());
cellNum++;
}
}
rowNum++;
}
adjustColumnSize(sheets, sheetNum, fieldNames); // 自动调整列宽
sheetNum++;
}
wb.write(outputStream);
outputStream.flush();
outputStream.close();
return outputStream;
}
/**
* @Description: 初始化
*/
private static void init() {
wb = new HSSFWorkbook();
titleFont = wb.createFont();
titleStyle = wb.createCellStyle();
dateStyle = wb.createCellStyle();
dateFont = wb.createFont();
headStyle = wb.createCellStyle();
headFont = wb.createFont();
contentStyle = wb.createCellStyle();
contentFont = wb.createFont();
initTitleCellStyle();
initTitleFont();
initDateCellStyle();
initDateFont();
initHeadCellStyle();
initHeadFont();
initContentCellStyle();
initContentFont();
}
/**
* @Description: 自动调整列宽
*/
private static void adjustColumnSize(HSSFSheet[] sheets, int sheetNum,
String[] fieldNames) {
for (int i = 0; i < fieldNames.length + 1; i++) {
sheets[sheetNum].autoSizeColumn(i, true);
}
}
/**
* @Description: 创建标题行(需合并单元格)
*/
private static void createTableTitleRow(ExcelExportData setInfo,
HSSFSheet[] sheets, int sheetNum) {
CellRangeAddress titleRange = new CellRangeAddress(0, 0, 0, setInfo
.getFieldNames().get(sheetNum).length);
sheets[sheetNum].addMergedRegion(titleRange);
HSSFRow titleRow = sheets[sheetNum].createRow(0);
titleRow.setHeight((short) 600);
HSSFCell titleCell = titleRow.createCell(0);
titleCell.setCellStyle(titleStyle);
titleCell.setCellValue(setInfo.getTitles()[sheetNum]);
}
/**
* @Description: 创建日期行(需合并单元格)
*/
private static void createTableDateRow(ExcelExportData setInfo,
HSSFSheet[] sheets, int sheetNum, String tjlx) {
CellRangeAddress dateRange = new CellRangeAddress(1, 1, 0, setInfo
.getFieldNames().get(sheetNum).length);
sheets[sheetNum].addMergedRegion(dateRange);
HSSFRow dateRow = sheets[sheetNum].createRow(1);
dateRow.setHeight((short) 350);
HSSFCell dateCell = dateRow.createCell(0);
dateCell.setCellStyle(dateStyle);
if (null==setInfo.getExportExcelDate()||"".equals(setInfo.getExportExcelDate())) {
dateCell.setCellValue("统计日期:"+new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
}else{
dateCell.setCellValue("统计日期:"+setInfo.getExportExcelDate());
}
//创建workbook
HSSFWorkbook workbook = new HSSFWorkbook();
//创建sheet页
//HSSFSheet sheet = workbook.createSheet("学生表");
//创建单元格
HSSFRow row = sheets[0].createRow(2);//---------------------第一行
row.setHeight((short) 350);
HSSFCell c0 = row.createCell(1);
if(tjlx.equals("aswjtj")){
c0.setCellValue(new HSSFRichTextString("税局"));
}else{
c0.setCellValue(new HSSFRichTextString("开票月份"));
}
c0.setCellStyle(dateStyle);
HSSFCell c1 = row.createCell(2);
c1.setCellValue(new HSSFRichTextString("数量(张)")); //1
c1.setCellStyle(dateStyle);
HSSFCell c2 = row.createCell(5);
c2.setCellValue(new HSSFRichTextString("金额(元)")); //2
c2.setCellStyle(dateStyle);
HSSFCell c3 = row.createCell(8);
c3.setCellValue(new HSSFRichTextString("税额(元)")); //3
c3.setCellStyle(dateStyle);
HSSFCell c4 = row.createCe