/**
* 写
* @param String fileName 被写入的Excel文件的路径
* @param String sheetName 被写入的Excel文件的工作表
* @param int location 被写入的Excel文件的工作表位于工作簿的位置
* @param List<Object[]> listData 数据库中读取的数据
* @throws Exception
*/
@Override
public int saveExcel(File file,String sheetName, int location, List<Ticket> listData) throws Exception {
// TODO Auto-generated method stub
HSSFWorkbook wb = new HSSFWorkbook();
//创建一个张表
HSSFSheet sheet = wb.createSheet(sheetName);
sheet.setColumnWidth(0, 25*256);
sheet.setColumnWidth(1, 25*256);
sheet.setColumnWidth(2, 25*256);
sheet.setColumnWidth(3, 25*256);
//创建第一行
HSSFRow row = sheet.createRow(0);
//创建第二行
HSSFRow row1 = sheet.createRow(1);
//设置字体
HSSFFont headFont = wb.createFont();
headFont.setFontName("微软雅黑");
headFont.setFontHeightInPoints((short)22);
headFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗
// 另一个样式
HSSFCellStyle headstyle = wb.createCellStyle();
headstyle.setFont(headFont);
headstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
headstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
headstyle.setLocked(true);
headstyle.setWrapText(true);// 自动换行
// 另一个字体样式
HSSFFont columnHeadFont = wb.createFont();
columnHeadFont.setFontName("宋体");
columnHeadFont.setFontHeightInPoints((short) 10);
columnHeadFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 列头的样式
HSSFCellStyle columnHeadStyle = wb.createCellStyle();
columnHeadStyle.setFont(columnHeadFont);
columnHeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
columnHeadStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
columnHeadStyle.setLocked(true);
columnHeadStyle.setWrapText(true);
columnHeadStyle.setLeftBorderColor(HSSFColor.BLACK.index);// 左边框的颜色
columnHeadStyle.setBorderLeft((short) 1);// 边框的大小
columnHeadStyle.setRightBorderColor(HSSFColor.BLACK.index);// 右边框的颜色
columnHeadStyle.setBorderRight((short) 1);// 边框的大小
columnHeadStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体
columnHeadStyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色
// 设置单元格的背景颜色(单元格的样式会覆盖列或行的样式)
columnHeadStyle.setFillForegroundColor(HSSFColor.WHITE.index);
HSSFFont font = wb.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 10);
// 普通单元格样式
HSSFCellStyle style = wb.createCellStyle();
style.setFont(font);
style.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 左右居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);// 上下居中
style.setWrapText(true);
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft((short) 1);
style.setRightBorderColor(HSSFColor.BLACK.index);
style.setBorderRight((short) 1);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体
style.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色.
style.setFillForegroundColor(HSSFColor.WHITE.index);// 设置单元格的背景颜色.
// 另一个样式
HSSFCellStyle centerstyle = wb.createCellStyle();
centerstyle.setFont(font);
centerstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
centerstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
centerstyle.setWrapText(true);
centerstyle.setLeftBorderColor(HSSFColor.BLACK.index);
centerstyle.setBorderLeft((short) 1);
centerstyle.setRightBorderColor(HSSFColor.BLACK.index);
centerstyle.setBorderRight((short) 1);
centerstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体
centerstyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色.
centerstyle.setFillForegroundColor(HSSFColor.WHITE.index);// 设置单元格的背景颜色.
//设置行高
row.setHeight((short)900);
HSSFCell cell0 = row.createCell(0);
cell0.setCellValue(new HSSFRichTextString("楷,可以让你回家的网站"));
cell0.setCellStyle(headstyle);
//合并第一行的单元格
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 3));
// 创建第二行
HSSFCell cell1 = row1.createCell(0);
cell1.setCellValue(new HSSFRichTextString("本次时间:"+new Date()) );
cell1.setCellStyle(centerstyle);
// 合并单元格
sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 3));
// 创建第三行
HSSFRow row2 = sheet.createRow(2);
row2.setHeight((short) 750);
HSSFCell cell2 = row2.createCell(0);
cell2.setCellValue(new HSSFRichTextString("订单编号"));
cell2.setCellStyle(columnHeadStyle);
cell2 = row2.createCell(1);
cell2.setCellValue(new HSSFRichTextString("始发站"));
cell2.setCellStyle(columnHeadStyle);
cell2 = row2.createCell(2);
cell2.setCellValue(new HSSFRichTextString("终点站"));
cell2.setCellStyle(columnHeadStyle);
cell2 = row2.createCell(3);
cell2.setCellValue(new HSSFRichTextString("发车时间"));
cell2.setCellStyle(columnHeadStyle);
//导入数据库中查询的数据
for(int i=0,row_num = 3;i<listData.size();i++){
Ticket ticket = listData.get(i);
HSSFRow row3 = sheet.createRow(row_num);
HSSFCell cell3 = row3.createCell(0);
cell3.setCellValue(ticket.getTicketnumber());
cell3.setCellStyle(style);
cell3 = row3.createCell(1);
cell3.setCellValue(ticket.getStartstation());
cell3.setCellStyle(style);
cell3 = row3.createCell(2);
cell3.setCellValue(ticket.getStopstation());
cell3.setCellStyle(style);
cell3 = row3.createCell(3);
cell3.setCellValue(ticket.getTime().toString());
cell3.setCellStyle(style);
row_num++;
}
FileOutputStream fileOutputStream = new FileOutputStream(file);
wb.write(fileOutputStream);
return 0;
}
poi如何去写入excel文件
最新推荐文章于 2024-02-16 16:37:09 发布