一、POI报表处理(Excel格式)
1.创建一个Excel表格文档:
Workbook wb = new HSSFWorkbook();
2.在Excel中创建一页
Sheet sheet = wb.createSheet("表格文档中一个页的名称");
3.在页中创建一行
Row row = sheet.createRow(下标序号);
Row row = sheet.getCell(下标序号); //返回该行对象
4.在行中创建单元格
Cell cell = row.createCell(下标序号); //在此行中创建新的单元格
Cell cell = row.getCell(下标序号); //返回该行中指定序号的单元格
5.设值
(1)基本类型的
cell.setCellValue(值);
(2)特殊类型的
日期:
CellStyle cellStyle = wb.createCellStyle();
CreationHelper createHelper = wb.getCreationHelper();
cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("yyyy-MM-dd")); //创出一个样式
cell.setCellValue(new Date());
cell.setCellStyle(cellStyle); //将前面的样式设在此格上
6.设单元格边框
cellStyle.setBorderBottom(CellStyle.BORDER_THIN); //设置下边框为淡线
cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); //设置下边框颜色为黑色
cellStyle.setBorderLeft(CellStyle.BORDER_THIN); //设置左边框为淡线
cellStyle.setLeftBorderColor(IndexedColors.GREEN.getIndex()); //设置左边框颜色为绿色
cellStyle.setBorderRight(CellStyle.BORDER_THIN); //设置右边框淡线
cellStyle.setRightBorderColor(IndexedColors.BLUE.getIndex()); //设置右边框颜色为蓝色
cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED); //设置上边框虚线
cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex()); //设置上边框颜色为黑色
cell.setCellStyle(style);
7.将Excel表格文档写入硬盘中
FileOutputStream fileOut = new FileOutputStream("e:\\Excel文档名.xls");
wb.write(fileOut);
fileOut.close();
Document document = new Document();
2.建立书写器与document关联
PdfWriter.getInstance();
3.打开文档
document.open();
4.在文档中文字
document.add(new Paragraph("文本"));
5.关闭文档
document.close();
1.创建一个Excel表格文档:
Workbook wb = new HSSFWorkbook();
2.在Excel中创建一页
Sheet sheet = wb.createSheet("表格文档中一个页的名称");
3.在页中创建一行
Row row = sheet.createRow(下标序号);
Row row = sheet.getCell(下标序号); //返回该行对象
4.在行中创建单元格
Cell cell = row.createCell(下标序号); //在此行中创建新的单元格
Cell cell = row.getCell(下标序号); //返回该行中指定序号的单元格
5.设值
(1)基本类型的
cell.setCellValue(值);
(2)特殊类型的
日期:
CellStyle cellStyle = wb.createCellStyle();
CreationHelper createHelper = wb.getCreationHelper();
cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("yyyy-MM-dd")); //创出一个样式
cell.setCellValue(new Date());
cell.setCellStyle(cellStyle); //将前面的样式设在此格上
6.设单元格边框
cellStyle.setBorderBottom(CellStyle.BORDER_THIN); //设置下边框为淡线
cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); //设置下边框颜色为黑色
cellStyle.setBorderLeft(CellStyle.BORDER_THIN); //设置左边框为淡线
cellStyle.setLeftBorderColor(IndexedColors.GREEN.getIndex()); //设置左边框颜色为绿色
cellStyle.setBorderRight(CellStyle.BORDER_THIN); //设置右边框淡线
cellStyle.setRightBorderColor(IndexedColors.BLUE.getIndex()); //设置右边框颜色为蓝色
cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED); //设置上边框虚线
cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex()); //设置上边框颜色为黑色
cell.setCellStyle(style);
7.将Excel表格文档写入硬盘中
FileOutputStream fileOut = new FileOutputStream("e:\\Excel文档名.xls");
wb.write(fileOut);
fileOut.close();
二、IText报表处理(pdf格式)
1.创建文档Document document = new Document();
2.建立书写器与document关联
PdfWriter.getInstance();
3.打开文档
document.open();
4.在文档中文字
document.add(new Paragraph("文本"));
5.关闭文档
document.close();