输入xls格式 输出 xls

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
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.com</groupId>
  <artifactId>excelReadAndWrite</artifactId>
  <version>0.0.1-SNAPSHOT</version>
   <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
	 <dependency>
	    <groupId>org.apache.directory.studio</groupId>
	    <artifactId>org.apache.commons.codec</artifactId>
	    <version>1.8</version>
	</dependency>
	 <dependency>
		<groupId>net.sourceforge.jexcelapi</groupId>
		<artifactId>jxl</artifactId>
		<version>2.6.12</version>
	</dependency>
	<dependency>
		<groupId>org.apache.poi</groupId>
		<artifactId>poi-ooxml</artifactId>
		<version>3.9</version>
	</dependency>
  </dependencies>
</project>

  

posted @ 2020-09-29 13:41  红尘沙漏  阅读(201)  评论(0编辑  收藏  举报