JAva excel回転csv

1744 ワード

これはjava言語でExcelファイルをcsvファイルに転送する簡単なdemoです.
package com.csv;



import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;


import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;


import com.monitorjbl.xlsx.StreamingReader;
//main       
public class CsvUtil {
	public static void main(String[] args) {
		csvs();
	}
	//   Excel csv   
	public static void csvs(){
		BufferedWriter bw=null;
	  Workbook wb=null;
	try {
	//  Excel         
	wb=StreamingReader.builder().bufferSize(4096).rowCacheSize(200).open(new FileInputStream("E://data/book.xlsx"));
	//  csv           ,     
	bw=	new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D://javalixiaobiao/MySQL/MySQL Server 5.6/Uploads/temp.csv"),"utf-8") );
	//      
		Sheet sheetAt = wb.getSheetAt(0);
	for (Row row : sheetAt) {
		for (Cell cell : row) {
			if(cell.getCellTypeEnum()==CellType.STRING){
				bw.write(cell.getStringCellValue());
			}
			if(cell.getCellTypeEnum()==CellType.NUMERIC){
				bw.write(""+cell.getNumericCellValue());
			}
			if(cell.getCellTypeEnum()==CellType.BOOLEAN){
				bw.write(""+cell.getBooleanCellValue());
			}
			
		}
		bw.newLine();
	}
	} catch (Exception e) {
		e.printStackTrace();
	}//  
	 finally{
		if(null!=wb){
			try {
				wb.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			if(null!=bw){
				try {
					bw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}
}
}
}