poi export excel

// Create an Excel file
HSSFWorkbook workbook = new HSSFWorkbook();
// Create a worksheet
HSSFSheet sheet = workbook.createSheet("Sales Order List");
// Add a header row
HSSFRow hssfRow = sheet.createRow(0);
// Set the cell format to center
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);

// Add header content
HSSFCell headCell = hssfRow.createCell(0);
headCell.setCellValue("Order Number") ;
headCell.setCellStyle(cellStyle);

headCell = hssfRow.createCell(1);
headCell.setCellValue("Product Number");
headCell.setCellStyle(cellStyle);

headCell = hssfRow.createCell(2);
headCell.setCellValue("Salesperson number");
headCell.setCellStyle(cellStyle);

// 添加数据内容
for (int i = 0; i < xsLists.size(); i++) {
hssfRow = sheet.createRow((int) i + 1);
XSList xsList = xsLists.get(i);

// 创建单元格,并设置值
HSSFCell cell = hssfRow.createCell(0);
cell.setCellValue(xsList.getSaleno());
cell.setCellStyle(cellStyle);

cell = hssfRow.createCell(1);
cell.setCellValue(xsList.getProductno());
cell.setCellStyle(cellStyle);

cell = hssfRow.createCell(2);
cell.setCellValue(xsList.getStaffno());
cell.setCellStyle(cellStyle);
}

// 保存Excel文件
try {
OutputStream outputStream = new FileOutputStream("E:/XSList.xls");
workbook.write(outputStream);
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325340901&siteId=291194637