poi导出Excel的两种方式

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

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

  78. 文章转自:http://quzhan87com.iteye.com/blog/854973 
0 0
原创粉丝点击