标签:
一、简介
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、示例代码如下:
package com.test.excel; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.xssf.streaming.SXSSFWorkbook; /** * *excle导出 */ public class ExcelCreate { /** * excel生成 */ public static void main(String[] args) throws Throwable { // 表头 List<String> headerList = new ArrayList<>(10); for (int i = 1; i <= 10; i++) { headerList.add("表头" + i); } // 数据 List<String[]> dataRowList2 = new ArrayList<String[]>(10); for (int i = 1; i <= headerList.size(); i++) { dataRowList2.add(new String[] { "数据" + i, "数据" + i, "数据" + i, "数据" + i, "数据" + i, "数据" + i, "数据" + i, "数据" + i, "数据" + i, "数据" + i }); } SXSSFWorkbook workbook = new SXSSFWorkbook(90); Sheet sheet = workbook.createSheet("Export"); int rownum = 0; // 表头 Row row = sheet.createRow(rownum++); for (int i = 0; i < headerList.size(); i++) { Cell cell = row.createCell(i); cell.setCellValue(headerList.get(i)); } // 数据 for (String[] array : dataRowList2) { row = sheet.createRow(rownum++); String[] arraya = (String[]) array; for (int i = 0; i < dataRowList2.size(); i++) { Cell cell = row.createCell(i); cell.setCellValue(arraya[i]); } } FileOutputStream os = new FileOutputStream("c:/test/testexcel.xlsx"); //以下代码可进行文件下载 // response.reset(); // response.setContentType("application/octet-stream; charset=utf-8"); // response.setHeader("Content-Disposition", "attachment; filename="+Encodes.urlEncode("testexcel.xlsx")); // write(response.getOutputStream()); workbook.write(os); workbook.dispose(); } }
3、运行结果如下:
标签:
原文地址:http://www.cnblogs.com/jinjixia/p/4816869.html