输入xls格式 输出 xls
阅读原文时间:2023年07月10日阅读:2

package com.cn.peitest.excel.word;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class xlsToxls {

public static void main(String\[\] args) throws Exception {

    //输入xls格式  
    POIFSFileSystem fs = null;  
    HSSFWorkbook xwb = null;  
    HSSFSheet xsheet = null;  
    HSSFRow xrow = null;  
    HSSFCell xcell = null;  
    //输出xls格式  
    HSSFWorkbook hwb = new HSSFWorkbook();  
    HSSFSheet hsheet =null;// wb.createSheet("sheet1");  
    HSSFRow hrow = null;//sheet.createRow(0);  
    HSSFCell hcell=null;//row0.createCell(0);  
    //cell0.setCellValue("0000");

    SimpleDateFormat df = new SimpleDateFormat("yyyy\_MM\_dd\_HH\_mm");//设置日期格式  
    System.out.println(df.format(new Date()));// new Date()为获取当前系统时间

    File inf=new File("C:\\\\Users\\\\pei\\\\Desktop\\\\新建 XLS 工作表.xls");  
    File ouf=new File("C:\\\\Users\\\\Pei\\\\Desktop\\\\HYD test 1\_0\_2 tm="+df.format(new Date())+".xls"); 

    boolean sheetflag=true;  
    int sheetid=0;

    boolean rowflag=true;  
    int rowid =0;  
    try {  
        FileInputStream fis = new FileInputStream(inf);  
        xwb=new HSSFWorkbook(fis);  
        System.out.println("表单数="+xwb.getNumberOfSheets());  
        while(sheetflag){  
            if(sheetid==xwb.getNumberOfSheets()){break;}  
            xsheet=xwb.getSheetAt(sheetid);  
            if(xsheet==null){  
                sheetflag=false;  
            }else{  
                hsheet=hwb.createSheet(xsheet.getSheetName());  
                sheetid++;  
                rowid=0;  
                rowflag=true;  
                while(rowflag){  
                    hrow=hsheet.createRow(rowid);  
                    xrow=xsheet.getRow(rowid);  
                    if(xrow==null){  
                        rowflag=false;  
                    }else{  
                        rowid++;  
                        for(int c=0;c<30;c++){

                            xcell=xrow.getCell(c);  
                            if(xcell!=null&&xcell.toString().length()>0){  
                                String a1=xcell.toString().replaceAll(".00", "");  
                                String a2=a1.replaceAll(".0", "");  
                                String data=a2.replaceAll("O", "0");  
                                hrow.createCell(c).setCellValue(data);  
                            }  
                        }  
                    }  
                }  
            }  
        }

        //FileOutputStream output=new FileOutputStream(ouf);  
        //xwb.write(output);  
        //System.out.println("导出结束");  
        //output.close();  
    } catch (Exception e) {  
        e.printStackTrace();  
    }  
    finally{  
        System.out.println("处理结束。。。。等待导出完成");  
        FileOutputStream output=new FileOutputStream(ouf);  
        hwb.write(output);  
        System.out.println("导出结束");  
        output.close();  
    }

}

}

//pom.xml
http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 cn.com excelReadAndWrite 0.0.1-SNAPSHOT junit junit 3.8.1 test org.apache.directory.studio org.apache.commons.codec 1.8 net.sourceforge.jexcelapi jxl 2.6.12 org.apache.poi poi-ooxml 3.9

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章