天天看點

Excel導出大量資料案例,poi導出案例

參考位址:https://www.cnblogs.com/silenceshining/p/11681924.html

maven:

<dependency>
  <groupId>net.sourceforge.jexcelapi</groupId>
  <artifactId>jxl</artifactId>
  <version>2.6.12</version>
</dependency>
<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi</artifactId>
   <version>3.17</version>
</dependency>
<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi-ooxml</artifactId>
   <version>3.17</version>
</dependency>
           

代碼:

package com.poi;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;

import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @describe:
 * @date: 2020/9/25
 * @TIME: 23:24
 */
public class ExcelTest {
    public static void main(String[] args) throws IOException {
        testHSSFWorkbook();
    }

    public static void testHSSFWorkbook() throws IOException {
        long start = System.currentTimeMillis();

        HSSFWorkbook workbook = new HSSFWorkbook();//建立excel檔案(workbook)
        //建立一個工作表sheet ”測試工作表1“是這個工作表的名稱,也可以不寫
        HSSFSheet sheet = workbook.createSheet("測試工作表1");
        HSSFRow row = sheet.createRow(0);//建立行 從0開始

        HSSFCellStyle style = workbook.createCellStyle();//設定單元格樣式
        //TODO 由于版本問題,新的版本劇中是使用HorizontalAlignment.CENTER水準居中 VerticalAlignment.CENTER垂直居中
        //style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水準居中
        //style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        style.setAlignment(HorizontalAlignment.CENTER);//水準居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sheet.setDefaultColumnWidth(40);
        row.setHeightInPoints(25);
        //30個表頭
        String[] top = new String[30];

        //設定表頭
        for (int i = 0; i < top.length; i++) {
            //sheet.setColumnWidth(i,20*256);//第一列設定為20個字元寬度
            HSSFCell cell = row.createCell(i);//建立行的單元格,從0開始
            cell.setCellValue("表頭" + i);//設定單元格内容
            cell.setCellStyle(style);
        }
        System.out.println("準備完表頭時間:" + (System.currentTimeMillis() - start));
        for (int i = 0; i < 50000; i++) {
            HSSFRow rowInfo = sheet.createRow(i + 1);
            rowInfo.setHeightInPoints(30);
            for (int j = 0; j < 30; j++) {
                HSSFCell cellInfo = rowInfo.createCell(j);
                cellInfo.setCellValue("列内容為" + j +"----"+format.format(new Date()));
                cellInfo.setCellStyle(style);

            }
        }
        System.out.println("準備完表内容時間:" + (System.currentTimeMillis() - start));
        FileOutputStream out = new FileOutputStream("D:\\test.xlsx");
        workbook.write(out);
        out.close();
        System.out.println("輸入完成時間:" + (System.currentTimeMillis() - start));
        System.out.println("finish");
    }
}
           

實作效果:

Excel導出大量資料案例,poi導出案例

繼續閱讀