You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.8 KiB
Java
65 lines
1.8 KiB
Java
package com.glxp.api.util;
|
|
|
|
import org.apache.poi.hssf.usermodel.*;
|
|
|
|
import java.io.File;
|
|
import java.io.FileOutputStream;
|
|
import java.util.List;
|
|
|
|
public class ExcelUtil {
|
|
|
|
|
|
public void exportExcel(
|
|
List<List<String>> excelData,
|
|
String sheetName,
|
|
String fileName,
|
|
int columnWidth) {
|
|
|
|
HSSFWorkbook workbook = new HSSFWorkbook();
|
|
HSSFSheet sheet = workbook.createSheet(sheetName);
|
|
sheet.setDefaultColumnWidth(columnWidth);
|
|
int rowIndex = 0;
|
|
for (List<String> data : excelData) {
|
|
HSSFRow row = sheet.createRow(rowIndex++);
|
|
for (int i = 0; i < data.size(); i++) {
|
|
HSSFCell cell = row.createCell(i);
|
|
String content = data.get(i);
|
|
if (content != null) {
|
|
HSSFRichTextString text = new HSSFRichTextString(content);
|
|
cell.setCellValue(text);
|
|
}else {
|
|
|
|
}
|
|
}
|
|
}
|
|
FileOutputStream outputStream = null;
|
|
try {
|
|
|
|
File createFile = new File(fileName);
|
|
if (!createFile.exists()) {
|
|
createFile.mkdirs();
|
|
}
|
|
outputStream = new FileOutputStream(fileName);
|
|
workbook.write(outputStream);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
} finally {
|
|
try {
|
|
if (outputStream != null) {
|
|
outputStream.flush();
|
|
outputStream.close();
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
try {
|
|
if (workbook != null) {
|
|
workbook.close();
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
}
|