poi生成Excel

来源:互联网 发布:成都黑帽seo方案 编辑:程序博客网 时间:2024/05/20 17:28
1、无模板生成Excel的方法
Java代码 复制代码 收藏代码
  1. package com.test;
  2. import java.io.FileOutputStream;
  3. import java.util.Date;
  4. import org.apache.poi.hssf.usermodel.HSSFCell;
  5. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  6. import org.apache.poi.hssf.usermodel.HSSFDataFormat;
  7. import org.apache.poi.hssf.usermodel.HSSFFont;
  8. import org.apache.poi.hssf.usermodel.HSSFHyperlink;
  9. import org.apache.poi.hssf.usermodel.HSSFRow;
  10. import org.apache.poi.hssf.usermodel.HSSFSheet;
  11. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  12. import org.apache.poi.hssf.util.HSSFColor;
  13. import org.apache.poi.ss.util.CellRangeAddress;
  14. public class PoiCreateExcel {
  15. public staticvoid main(String[] args) throws Exception {
  16. // 创建Excel的工作书册 Workbook,对应到一个excel文档
  17. HSSFWorkbook wb = new HSSFWorkbook();
  18. // 创建Excel的工作sheet,对应到一个excel文档的tab
  19. HSSFSheet sheet = wb.createSheet("sheet1");
  20. // 设置excel每列宽度
  21. sheet.setColumnWidth(0,4000);
  22. sheet.setColumnWidth(1,3500);
  23. // 创建字体样式
  24. HSSFFont font = wb.createFont();
  25. font.setFontName("Verdana");
  26. font.setBoldweight((short) 100);
  27. font.setFontHeight((short) 300);
  28. font.setColor(HSSFColor.BLUE.index);
  29. // 创建单元格样式
  30. HSSFCellStyle style = wb.createCellStyle();
  31. style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
  32. style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
  33. style.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index);
  34. style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
  35. // 设置边框
  36. style.setBottomBorderColor(HSSFColor.RED.index);
  37. style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
  38. style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
  39. style.setBorderRight(HSSFCellStyle.BORDER_THIN);
  40. style.setBorderTop(HSSFCellStyle.BORDER_THIN);
  41. style.setFont(font);// 设置字体
  42. // 创建Excel的sheet的一行
  43. HSSFRow row = sheet.createRow(0);
  44. row.setHeight((short) 500);// 设定行的高度
  45. // 创建一个Excel的单元格
  46. HSSFCell cell = row.createCell(0);
  47. // 合并单元格(startRow,endRow,startColumn,endColumn)
  48. sheet.addMergedRegion(new CellRangeAddress(0,0, 0, 2));
  49. // 给Excel的单元格设置样式和赋值
  50. cell.setCellStyle(style);
  51. cell.setCellValue("hello world");
  52. // 设置单元格内容格式
  53. HSSFCellStyle style1 = wb.createCellStyle();
  54. style1.setDataFormat(HSSFDataFormat.getBuiltinFormat("h:mm:ss"));
  55. style1.setWrapText(true);// 自动换行
  56. row = sheet.createRow(1);
  57. // 设置单元格的样式格式
  58. cell = row.createCell(0);
  59. cell.setCellStyle(style1);
  60. cell.setCellValue(new Date());
  61. // 创建超链接
  62. HSSFHyperlink link = new HSSFHyperlink(HSSFHyperlink.LINK_URL);
  63. link.setAddress("http://www.baidu.com");
  64. cell = row.createCell(1);
  65. cell.setCellValue("百度");
  66. cell.setHyperlink(link);// 设定单元格的链接
  67. FileOutputStream os = new FileOutputStream("D:\\report\\workbook.xls");
  68. wb.write(os);
  69. os.close();
  70. }
  71. }

注:HSSFWorkbook,XSSFWorkbook的区别:前者是解析出来excel 2007 以前版本的,后缀名为xls的,后者是解析excel 2007 版的,后缀名为xlsx。

文章来源:http://hi.baidu.com/suny%5Fduan/blog/item/d528d5112b03cff0c3ce79a6.html
2、有模板,文件头标题都写好,只要循环添加数据的方法
Java代码 复制代码 收藏代码
  1. package com.test;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  8. import org.apache.poi.ss.usermodel.Cell;
  9. import org.apache.poi.ss.usermodel.CellStyle;
  10. import org.apache.poi.ss.usermodel.Row;
  11. import org.apache.poi.ss.usermodel.Sheet;
  12. import org.apache.poi.ss.usermodel.Workbook;
  13. import org.apache.poi.ss.util.CellRangeAddress;
  14. public class PoiTestExcel {
  15. /**
  16. * @param args
  17. */
  18. public staticvoid main(String[] args) {
  19. // TODO Auto-generated method stub
  20. try {
  21. InputStream in = new FileInputStream("D:\\report\\1111.xls");
  22. Workbook work = new HSSFWorkbook(in);
  23. // 得到excel的第0张表
  24. Sheet sheet = work.getSheetAt(0);
  25. // 得到第1行的第一个单元格的样式
  26. Row rowCellStyle = sheet.getRow(1);
  27. CellStyle columnOne = rowCellStyle.getCell(0).getCellStyle();
  28. // 这里面的行和列的数法与计算机里的一样,从0开始是第一
  29. // 填充title数据
  30. Row row = sheet.getRow(0);
  31. Cell cell = row.getCell(0);
  32. cell.setCellValue("2010年花名测");
  33. int i = 2;//计数器
  34. int number = 0;
  35. // 得到行,并填充数据和表格样式
  36. for (;i < 10; i++) {
  37. row = sheet.createRow(i);// 得到行
  38. cell = row.createCell(0);// 得到第0个单元格
  39. cell.setCellValue("琳"+i);// 填充值
  40. cell.setCellStyle(columnOne);// 填充样式
  41. cell = row.createCell(1);
  42. cell.setCellValue("女");
  43. cell.setCellStyle(columnOne);// 填充样式
  44. cell = row.createCell(2);
  45. cell.setCellValue(i+20);
  46. cell.setCellStyle(columnOne);// 填充样式
  47. // .....给每个单元格填充数据和样式
  48. number++;
  49. }
  50. //创建每个单元格,添加样式,最后合并
  51. row = sheet.createRow(i);
  52. cell = row.createCell(0);
  53. cell.setCellValue("总计:" + number +"个学生");// 填充值
  54. cell.setCellStyle(columnOne);
  55. cell = row.createCell(1);
  56. cell.setCellStyle(columnOne);
  57. cell = row.createCell(2);
  58. cell.setCellStyle(columnOne);
  59. // 合并单元格
  60. sheet.addMergedRegion(new CellRangeAddress(i,i,0,2));
  61. FileOutputStream os = new FileOutputStream("D:\\report\\workbook.xls");
  62. work.write(os);
  63. os.close();
  64. } catch (FileNotFoundException e) {
  65. System.out.println("文件路径错误");
  66. e.printStackTrace();
  67. } catch (IOException e) {
  68. System.out.println("文件输入流错误");
  69. e.printStackTrace();
  70. }
  71. }
  72. }
0 0