POI는 생성된 EXCEL에 여러 번 쓰기 가능
1: EXCEL에 데이터를 여러 번 쓰기
1: 이런 방식은 효율이 비교적 낮고 데이터량이 많을수록 뚜렷하다. 4만 개의 데이터는 2분 정도 걸린다package com.test;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class POIController {
/**
* , ,4 2
* @param args
* @throws FileNotFoundException
* @throws InvalidFormatException
*/
public static void main(String[] args) throws FileNotFoundException, InvalidFormatException {
long startTime = System.currentTimeMillis();
BufferedOutputStream outPutStream = null;
XSSFWorkbook workbook = null;
FileInputStream inputStream = null;
String filePath = "E:\\txt\\111.xlsx";
try {
workbook = getWorkBook(filePath);
XSSFSheet sheet = workbook.getSheetAt(0);
for (int i = 0; i < 40; i++) {
for (int z = 0; z < 1000; z++) {
XSSFRow row = sheet.createRow(i*1000+z);
for (int j = 0; j < 10; j++) {
row.createCell(j).setCellValue(" :"+j);
}
}
// ,
outPutStream = new BufferedOutputStream(new FileOutputStream(filePath));
workbook.write(outPutStream);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(outPutStream!=null) {
try {
outPutStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(inputStream!=null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(workbook!=null) {
try {
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long endTime = System.currentTimeMillis();
System.out.println(endTime-startTime);
}
/**
* XSSFWorkbook
* @param filePath
* @return
*/
public static XSSFWorkbook getWorkBook(String filePath) {
XSSFWorkbook workbook = null;
try {
File fileXlsxPath = new File(filePath);
BufferedOutputStream outPutStream = new BufferedOutputStream(new FileOutputStream(fileXlsxPath));
workbook = new XSSFWorkbook();
workbook.createSheet(" ");
workbook.write(outPutStream);
} catch (Exception e) {
e.printStackTrace();
}
return workbook;
}
}
2: 이러한 방식은 효율이 높고 일회용 쓰기에 속하며 데이터 양이 클수록 뚜렷하다. 4만 개의 데이터는 17초 정도 걸린다package com.test;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class POIController {
/**
* , , ,4 17
* @param args
* @throws FileNotFoundException
* @throws InvalidFormatException
*/
public static void main(String[] args) throws FileNotFoundException, InvalidFormatException {
long startTime = System.currentTimeMillis();
BufferedOutputStream outPutStream = null;
XSSFWorkbook workbook = null;
FileInputStream inputStream = null;
String filePath = "E:\\txt\\111.xlsx";
try {
workbook = getWorkBook(filePath);
XSSFSheet sheet = workbook.getSheetAt(0);
for (int i = 0; i < 40; i++) {
for (int z = 0; z < 1000; z++) {
XSSFRow row = sheet.createRow(i*1000+z);
for (int j = 0; j < 10; j++) {
row.createCell(j).setCellValue(" :"+j);
}
}
}
// ,
outPutStream = new BufferedOutputStream(new FileOutputStream(filePath));
workbook.write(outPutStream);
} catch (IOException e) {
e.printStackTrace();
}finally {
if(outPutStream!=null) {
try {
outPutStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(inputStream!=null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(workbook!=null) {
try {
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long endTime = System.currentTimeMillis();
System.out.println(endTime-startTime);
}
/**
* XSSFWorkbook
* @param filePath
* @return
*/
public static XSSFWorkbook getWorkBook(String filePath) {
XSSFWorkbook workbook = null;
try {
File fileXlsxPath = new File(filePath);
BufferedOutputStream outPutStream = new BufferedOutputStream(new FileOutputStream(fileXlsxPath));
workbook = new XSSFWorkbook();
workbook.createSheet(" ");
workbook.write(outPutStream);
} catch (Exception e) {
e.printStackTrace();
}
return workbook;
}
}
다음: POI의 SXSSFWorkbook 대량의 데이터를 excel 문서 디렉터리로 내보내기
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Java POI로 Word(.docx) 파일 만들기
사전에 다음 라이브러리를 준비합니다.
Apache POI
※"poi-bin-3.16-20170419.tar.gz"의 링크에서 다운로드
이번 샘플은 이하의 jar가 있으면 동작합니다.
poi-3.16.jar
poi-o...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.
package com.test;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class POIController {
/**
* , ,4 2
* @param args
* @throws FileNotFoundException
* @throws InvalidFormatException
*/
public static void main(String[] args) throws FileNotFoundException, InvalidFormatException {
long startTime = System.currentTimeMillis();
BufferedOutputStream outPutStream = null;
XSSFWorkbook workbook = null;
FileInputStream inputStream = null;
String filePath = "E:\\txt\\111.xlsx";
try {
workbook = getWorkBook(filePath);
XSSFSheet sheet = workbook.getSheetAt(0);
for (int i = 0; i < 40; i++) {
for (int z = 0; z < 1000; z++) {
XSSFRow row = sheet.createRow(i*1000+z);
for (int j = 0; j < 10; j++) {
row.createCell(j).setCellValue(" :"+j);
}
}
// ,
outPutStream = new BufferedOutputStream(new FileOutputStream(filePath));
workbook.write(outPutStream);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(outPutStream!=null) {
try {
outPutStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(inputStream!=null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(workbook!=null) {
try {
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long endTime = System.currentTimeMillis();
System.out.println(endTime-startTime);
}
/**
* XSSFWorkbook
* @param filePath
* @return
*/
public static XSSFWorkbook getWorkBook(String filePath) {
XSSFWorkbook workbook = null;
try {
File fileXlsxPath = new File(filePath);
BufferedOutputStream outPutStream = new BufferedOutputStream(new FileOutputStream(fileXlsxPath));
workbook = new XSSFWorkbook();
workbook.createSheet(" ");
workbook.write(outPutStream);
} catch (Exception e) {
e.printStackTrace();
}
return workbook;
}
}
package com.test;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class POIController {
/**
* , , ,4 17
* @param args
* @throws FileNotFoundException
* @throws InvalidFormatException
*/
public static void main(String[] args) throws FileNotFoundException, InvalidFormatException {
long startTime = System.currentTimeMillis();
BufferedOutputStream outPutStream = null;
XSSFWorkbook workbook = null;
FileInputStream inputStream = null;
String filePath = "E:\\txt\\111.xlsx";
try {
workbook = getWorkBook(filePath);
XSSFSheet sheet = workbook.getSheetAt(0);
for (int i = 0; i < 40; i++) {
for (int z = 0; z < 1000; z++) {
XSSFRow row = sheet.createRow(i*1000+z);
for (int j = 0; j < 10; j++) {
row.createCell(j).setCellValue(" :"+j);
}
}
}
// ,
outPutStream = new BufferedOutputStream(new FileOutputStream(filePath));
workbook.write(outPutStream);
} catch (IOException e) {
e.printStackTrace();
}finally {
if(outPutStream!=null) {
try {
outPutStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(inputStream!=null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(workbook!=null) {
try {
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long endTime = System.currentTimeMillis();
System.out.println(endTime-startTime);
}
/**
* XSSFWorkbook
* @param filePath
* @return
*/
public static XSSFWorkbook getWorkBook(String filePath) {
XSSFWorkbook workbook = null;
try {
File fileXlsxPath = new File(filePath);
BufferedOutputStream outPutStream = new BufferedOutputStream(new FileOutputStream(fileXlsxPath));
workbook = new XSSFWorkbook();
workbook.createSheet(" ");
workbook.write(outPutStream);
} catch (Exception e) {
e.printStackTrace();
}
return workbook;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Java POI로 Word(.docx) 파일 만들기사전에 다음 라이브러리를 준비합니다. Apache POI ※"poi-bin-3.16-20170419.tar.gz"의 링크에서 다운로드 이번 샘플은 이하의 jar가 있으면 동작합니다. poi-3.16.jar poi-o...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.