您好,登錄后才能下訂單哦!
這是一個導(dǎo)出數(shù)據(jù)到Excel的工具類
<!-- poi配置開始 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.0</version>
</dependency>
<!-- poi-ooxml配置開始 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.1</version>
</dependency>
commons-collections4-4.2.jar
commons-compress-1.18.jar
poi-4.0.0.jar
poi-ooxml-4.0.0.jar
poi-ooxml-schemas-4.0.0.jar
xmlbeans-3.0.1.jar
工具類代碼
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
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;
public class ToolExcelExp {
/**
* @描述: 導(dǎo)出信息到Excel文件
*/
public static boolean exportBigDataExcel(String savePath, List<String[]> dataList) {
long startTime = System.currentTimeMillis();
boolean flag = false;
// 最重要的就是使用SXSSFWorkbook,表示流的方式進行操作
// 在內(nèi)存中保持100行,超過100行將被刷新到磁盤
SXSSFWorkbook wb = new SXSSFWorkbook(100);
Sheet sh = wb.createSheet(); // 建立新的sheet對象
for (int i = 0, dataListSize = dataList.size(); i < dataListSize; i++) {
String[] str = dataList.get(i);
Row row_value = sh.createRow(i);
for (int j = 0, strLength = str.length; j < strLength; j++) {
Cell cel_value = row_value.createCell(j);
cel_value.setCellValue(str[j]);
}
}
FileOutputStream fileOut;
try {
File fileDir = new File(savePath.substring(0, savePath.lastIndexOf(File.separator)));
if (!fileDir.exists()) {
fileDir.mkdirs();
}
fileOut = new FileOutputStream(savePath);
wb.write(fileOut);
fileOut.close();
wb.dispose();
wb.close();
flag = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
long endTime = System.currentTimeMillis();
System.out.println("導(dǎo)出完畢,共耗時" + (endTime-startTime) + "毫秒");
return flag;
}
// 生成測試數(shù)據(jù)
private static List<String[]> createData (){
List<String[]> dataList = new ArrayList<String[]>();
String[] headStr = new String[8];
headStr[0] = "標(biāo)題1";
headStr[1] = "標(biāo)題2";
headStr[2] = "標(biāo)題3";
headStr[3] = "標(biāo)題4";
headStr[4] = "標(biāo)題5";
headStr[5] = "標(biāo)題6";
headStr[6] = "標(biāo)題7";
headStr[7] = "標(biāo)題8";
dataList.add(headStr);
for (int i = 0; i < 1000000; i++) {
String[] str = new String[8];
str[0] = "數(shù)據(jù)的垃圾分類時間到了開房記錄圣誕節(jié)了激烈的看見了房間里都是1";
str[1] = "sdfsdfdsfdsfffffffffffff2";
str[2] = "320480098308409823093";
str[3] = "sdjlfkjl立刻就餓了接口連接的4";
str[4] = "6465213的方式多為sdfsdf5";
str[5] = "水電費水電費水電費水電費收到6";
str[6] = "而和任何任務(wù)而氣溫氣溫7";
str[7] = "ssdljfls了解惡劣螺紋孔8";
dataList.add(str);
}
return dataList;
}
public static void main(String[] args) {
List<String[]> dataList = createData();
exportBigDataExcel("e:\111.xlsx", dataList);
}
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。