/**
* 把ComparisonData对象转换为Excel文件
*@author luoyi
* @param list
* @param filename
* @param ComparisonDatarows
* @param session
*/
public void createExcel(List<ComparisonData> list, String filename, String photoFileName, String[] ComparisonDatarows, HttpSession session) {
HSSFWorkbook wkb = new HSSFWorkbook();
//创建一个样式
HSSFCellStyle cellStyle = wkb.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
// 设置边框
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
// 自动换行
// cellStyle.setWrapText(true);
//创建字体
HSSFFont font = wkb.createFont();
font.setFontHeightInPoints((short) 10);
font.setColor(HSSFColor.BLACK.index);
font.setFontName("宋体");
// 把字体 应用到当前样式
cellStyle.setFont(font);
//格式化单元格日期信息
HSSFDataFormat dataFormat = wkb.createDataFormat();
short dataformat = dataFormat.getFormat("yyyy-mm-dd");
cellStyle.setDataFormat(dataformat);
//建立新的sheet对象(excel的表单)
HSSFSheet sheet = wkb.createSheet("人员表");
//在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个
HSSFRow row1 = sheet.createRow(0);
for (int i = 0; i < ComparisonDatarows.length; i++) {
HSSFCell ce = row1.createCell(i);
ce.setCellStyle(cellStyle);
ce.setCellValue(ComparisonDataConvention.getComparisonDataRowName(ComparisonDatarows[i]));
}
List<String> oldFiles = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
HSSFRow row = sheet.createRow(i + 1);
for (int j = 0; j < ComparisonDatarows.length; j++) {
HSSFCell cell = row.createCell(j);
cell.setCellStyle(cellStyle);
String comparisonDataRow = getComparisonDataRow(ComparisonDatarows[j], list.get(i));
cell.setCellValue(comparisonDataRow);
}
if (i % 100 == 0) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (String comparisonDatarow : ComparisonDatarows) {
if ("authphoto".equals(comparisonDatarow)) {
String authphoto = list.get(i).getAuthphoto();
if (StringUtils.isNotBlank(authphoto)) {
oldFiles.add(authphoto);
}
}
if ("personphoto".equals(comparisonDatarow)) {
String personphoto = list.get(i).getPersonphoto();
if (StringUtils.isNotBlank(personphoto)) {
oldFiles.add(personphoto);
}
}
if ("photo".equals(comparisonDatarow)) {
String photo = list.get(i).getPhoto();
if (StringUtils.isNotBlank(photo)) {
oldFiles.add(photo);
}
}
}
}
//调整列的宽度
for (int i = 0; i < ComparisonDatarows.length; i++) {
sheet.autoSizeColumn(i);
}
setSizeColumn(sheet,ComparisonDatarows.length);
String path = session.getServletContext().getRealPath("/") + Convention.EXCEL_EXPLODE_PATH+ File.separator+photoFileName;
File pathFile = new File(path);
if (!pathFile.exists()) {
pathFile.mkdirs();
}
FileUtils.copyFolder(oldFiles,path,photoFileName);
File file = new File(path + File.separator + filename);
try {
if (!file.exists()) {
file.createNewFile();
}
OutputStream out = new FileOutputStream(file);
wkb.write(out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
我们使用autoSizeColumn方法可以把Excel设置为根据内容自动调整列宽,然而这个方法对中文并不起效,只对数字和字母有效;
private void setSizeColumn(HSSFSheet sheet, int size) {
for (int columnNum = 0; columnNum < size; columnNum++) {
int columnWidth = sheet.getColumnWidth(columnNum) / 256;
for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
HSSFRow currentRow;
//当前行未被使用过
if (sheet.getRow(rowNum) == null) {
currentRow = sheet.createRow(rowNum);
} else {
currentRow = sheet.getRow(rowNum);
}
if (currentRow.getCell(columnNum) != null) {
HSSFCell currentCell = currentRow.getCell(columnNum);
if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
int length = currentCell.getStringCellValue().getBytes().length+5;
if (columnWidth < length) {
columnWidth = length;
}
}
}
}
sheet.setColumnWidth(columnNum, columnWidth * 256);
}
}
https://blog.csdn.net/Pro_Mophy/article/details/81626119