java输出excel数据的方法示例(一种是到excel,一种是结构化数据到文本文件,可以用editplus之类的看)

来源:互联网 发布:python为什么叫爬虫 编辑:程序博客网 时间:2024/05/11 22:39

 最近需要输出大量数据,但是poi 03支持数据有限,于是使用支持数据更多的poi新版本,使用SXSSFWorkbook类,可以输出大量数据。

不过如果需要输出格式,就用excel,后来我发现我只是需要输出结构化数据,不需要复杂的格式设置,那BufferedWriter文件流输出貌似更加合适,简便,高效。


代码我是参考网上的,忘了出处,如有异议,请通知删除,谢谢。


import java.io.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();
}
   
   
   
   
    }
   
   
   
    }

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 医生说来姨妈不可以用卫生巾怎么办 隆胸以后摸起来感觉假体会动怎么办 产后15个月说恶露没排干净怎么办 母猪产后两天肚子里还有小猪怎么办 背心式无痕运动文胸显得胸小怎么办 卫生巾过敏起疙瘩反复挠不好怎么办 去健身房办卡老板跑了怎么办 买货我已经拒收商家不退款怎么办 在京东买了东西拒收不退款怎么办 罗马仕充电宝进入休眠状态怎么办 广发信用卡寄到家没拿到快递怎么办 包邮商家要买家出物流费怎么办? 美团外卖下单转化率低怎么办 京东退款不小心点了取消退款怎么办 近邻宝开了箱又关了怎么办 近邻宝箱子打开了东西忘记拿怎么办 京东取消订单商家总不取消怎么办 京东快递退货取件一直取不到怎么办 在京东线上付款了但没收到货怎么办 寄快递收件人电话号码写错了怎么办 京东购物实名认证被别人占用怎么办 货已发出单号还没填买家退货怎么办 自提柜还有一个包忘记拿怎么办 当顾客说衣服太贵的时候怎么办 京东快递送错了被别人签收了怎么办 乐视1s玩王者荣耀卡怎么办 京东商城买东西发现地址错了怎么办 京东分期付款买手机额度不够怎么办 亮皮银色高跟鞋时间放久变色怎么办 饿了么商家钱包提现被锁了怎么办 在淘手游买的游戏账号被找回怎么办 交易猫买手游梦幻号被找回了怎么办 绑定着苹果账号的邮箱忘记了怎么办 京东注册账号跟密码都忘记了怎么办 孩子大学一个宿舍宿友不行怎么办 微信手机支付密码忘记了怎么办 微信公众号不能付款了怎么办 头条误点了投放头条广告怎么办 交行网银密码错误6次怎么办 宁波新冮厦关门我们的消费卡怎么办 第一天上班别人教的学不会怎么办