通过指定列头集合创建EXCEL模板(初始化EXCEL模板为文本格式)
/**
* 根据列头创建Excel模板
*
* @param headList
* @return
*/
public static XSSFWorkbook createWorkBook(List<String> headList) {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet("数据表");
//设置excel为文本格式
XSSFCellStyle cellStyle = wb.createCellStyle();
XSSFDataFormat dataFormat = wb.createDataFormat();
cellStyle.setDataFormat(dataFormat.getFormat("@"));
for(int column=0;column<=headList.size();column++){
sheet.setDefaultColumnStyle(column,cellStyle);
}
buildHeader(sheet, headList);
return wb;
}
/**
* 创建表头
*
* @param sheet
*/
private static void buildHeader(XSSFSheet sheet, List<String> headList) {
XSSFRow headerRow = sheet.createRow(0);
int index = 0;
for (String header : headList) {
XSSFCell cell = headerRow.createCell(index, Cell.CELL_TYPE_STRING);
cell.setCellValue(header);
index++;
}
}
获取当前单元格内容
/**
* 获取当前单元格内容
*/
public static String getCellValue(Cell cell) {
String value = "";