/**
* 把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