标签:
一、简介
HSSF 是POI项目的纯的Java的Excel97(-2007)文件格式的实现。XSSF是POI 项目的Excel 2007 OOXML (.xlsx)文件格式的Java实现。HSSF and XSSF提供多种方式来阅读电子表格创建、修改、读取和写入XLS电子表格。
它提供如下功能:
项目网址如下:http://poi.apache.org/spreadsheet/index.html 。
二、实例
1、相关依赖如下:
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.12</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.12</version> </dependency>
2、示例代码如下:
1 package com.test.excel; 2 3 import java.io.FileOutputStream; 4 import java.util.ArrayList; 5 import java.util.List; 6 7 import org.apache.poi.ss.usermodel.Cell; 8 import org.apache.poi.ss.usermodel.Row; 9 import org.apache.poi.ss.usermodel.Sheet; 10 import org.apache.poi.xssf.streaming.SXSSFWorkbook; 11 12 /** 13 * 14 *excle导出 15 */ 16 public class ExcelCreate { 17 18 /** 19 * excel生成 20 */ 21 public static void main(String[] args) throws Throwable { 22 // 表头 23 List<String> headerList = new ArrayList<>(10); 24 for (int i = 1; i <= 10; i++) { 25 headerList.add("表头" + i); 26 } 27 28 // 数据 29 List<String[]> dataRowList2 = new ArrayList<String[]>(10); 30 for (int i = 1; i <= headerList.size(); i++) { 31 dataRowList2.add(new String[] { "数据" + i, "数据" + i, "数据" + i, "数据" + i, "数据" + i, "数据" + i, "数据" + i, "数据" + i, "数据" + i, "数据" + i }); 32 } 33 34 SXSSFWorkbook workbook = new SXSSFWorkbook(90); 35 Sheet sheet = workbook.createSheet("Export"); 36 37 int rownum = 0; 38 // 表头 39 Row row = sheet.createRow(rownum++); 40 for (int i = 0; i < headerList.size(); i++) { 41 Cell cell = row.createCell(i); 42 cell.setCellValue(headerList.get(i)); 43 } 44 45 // 数据 46 for (String[] array : dataRowList2) { 47 row = sheet.createRow(rownum++); 48 String[] arraya = (String[]) array; 49 for (int i = 0; i < dataRowList2.size(); i++) { 50 Cell cell = row.createCell(i); 51 cell.setCellValue(arraya[i]); 52 } 53 } 54 55 56 FileOutputStream os = new FileOutputStream("c:/test/testexcel.xlsx"); 57 58 //以下代码可进行文件下载 59 // response.reset(); 60 // response.setContentType("application/octet-stream; charset=utf-8"); 61 // response.setHeader("Content-Disposition", "attachment; filename="+Encodes.urlEncode("testexcel.xlsx")); 62 // write(response.getOutputStream()); 63 64 workbook.write(os); 65 workbook.dispose(); 66 } 67 }
3、运行结果如下:
标签:
原文地址:http://www.cnblogs.com/always-online/p/4801480.html