最近需要输出大量数据,但是poi 03支持数据有限,于是使用支持数据更多的poi新版本,使用SXSSFWorkbook类,可以输出大量数据。
不过如果需要输出格式,就用excel,后来我发现我只是需要输出结构化数据,不需要复杂的格式设置,那BufferedWriter文件流输出貌似更加合适,简便,高效。
代码我是参考网上的,忘了出处,如有异议,请通知删除,谢谢。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.Date;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class align_excel{
//注意输出循环的时候,循环体内部千万不能出现新建对象的操作,不然内存很容易溢出;
public static void main(String[] args) throws Exception {
//100行记录存在内存中,这个数字可以改,写入磁盘的数据不能修改;
Workbook wb = new SXSSFWorkbook(100);//XSSFWorkbook(); //or new HSSFWorkbook();三种格式有两种是一样的,hssf是03版本,支持少。
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("d:/xssf-align.xlsx");
Sheet sheet = wb.createSheet();
Row row=sheet.createRow((int)1);
CellStyle cellStyle = wb.createCellStyle();
Cell cell=row.createCell(1);
for (int i=1; i<1000000; i++){
row = sheet.createRow((int) i);
// row.setHeightInPoints(30);
createCell(cellStyle,cell,wb, row, (short) 0, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_BOTTOM);
createCell(cellStyle,cell,wb, row, (short) 1, CellStyle.ALIGN_CENTER_SELECTION, CellStyle.VERTICAL_BOTTOM);
createCell(cellStyle,cell,wb, row, (short) 2, CellStyle.ALIGN_FILL, CellStyle.VERTICAL_CENTER);
createCell(cellStyle,cell,wb, row, (short) 3, CellStyle.ALIGN_GENERAL, CellStyle.VERTICAL_CENTER);
createCell(cellStyle,cell,wb, row, (short) 4, CellStyle.ALIGN_JUSTIFY, CellStyle.VERTICAL_JUSTIFY);
createCell(cellStyle,cell,wb, row, (short) 5, CellStyle.ALIGN_LEFT, CellStyle.VERTICAL_TOP);
createCell(cellStyle,cell,wb, row, (short) 6, CellStyle.ALIGN_RIGHT, CellStyle.VERTICAL_TOP);
// wb.write(fileOut);
}
//创建不同类型的单元格
// Workbook wb = new HSSFWorkbook();
//Sheet sheet = wb.createSheet("new sheet");
row = sheet.createRow((int)1000005);
row.createCell(0).setCellValue(1.1);
row.createCell(1).setCellValue(new Date());
row.createCell(2).setCellValue(Calendar.getInstance());
row.createCell(3).setCellValue("这段代码是添加不同类型的单元格格式");
row.createCell(4).setCellValue(true);
row.createCell(5).setCellType(Cell.CELL_TYPE_ERROR);
wb.write(fileOut);
fileOut.close();
wb.close();
// read excel
readexcel();
//这种方法节省内存,存入更多的数据;
writestrems();
}
/**
* Creates a cell and aligns it a certain way.
*
* @param wb the workbook
* @param row the row to create the cell in
* @param column the column number to create the cell in
* @param halign the horizontal alignment for the cell.
*/
private static Cell createCell(CellStyle cellStyle,Cell cell,Workbook wb, Row row, short column, short halign, short valign) {
cell= row.createCell(column);
cell.setCellValue("Align It");
cellStyle.setAlignment(halign);
cellStyle.setVerticalAlignment(valign);
cell.setCellStyle(cellStyle);
return cell;
}
private static void readexcel(){
// Use a file
// Workbook wb = WorkbookFactory.create(new File("D:/test.xls"));
// Use an InputStream, needs more memory
try {
Workbook wb1 = WorkbookFactory.create(new FileInputStream("D:/test.xlsx"));
Sheet sheet1=wb1.getSheetAt(0);
Row row=sheet1.getRow(0);
Cell cell =row.getCell(0);
System.out.println(cell.getNumericCellValue());
wb1.close();
} catch (EncryptedDocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//这个方法更加简便,如果单纯结构化数据输出,不需要excel格式的话,可以用这个方法;
public static void writestrems() throws FileNotFoundException{
File file= new File("D:\\stremExcel.txt");
if (file.isFile()){
file.mkdir();}
FileOutputStream out=new FileOutputStream(file);
OutputStreamWriter osw;
try {
osw = new OutputStreamWriter(out,"GB2312");
BufferedWriter bw =new BufferedWriter(osw);
bw.write("res\tres\tres\tres\tres\t\r\n");
for(int i=0;i<1000000;i++){
bw.write("res\tres\tres\tres\tres\t\r\n");
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}