CSV XLS Converter In Java (JXL Java Excel API)

近日工作清闲, 整理一些小小知识点分享和备用.

CSV(Comma Separated Value), 逗号分隔纯文本文件, 用来存储数据.

XLS Microsoft excel. csv文件比存储同样文本数据的excel要小很多.

可以打开excel然后选择另存为csv文件.

因为很多时候我们的数据来源是CSV文件, 但是Java API没有直接操作CSV文件的, 我们需要把csv文件先转换为xls文件.

直接贴源代码了, 代码需要JXL库支持.

JXL下载地址: http://sourceforge.net/project/showfiles.php?group_id=79926

 

下面直接贴源码:

XlsProcessor.java -- convert cvs to xls, compare two cvs file contents

 

/*
 * Created on 2009-4-1
 */
package rt;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

/**
 *
 *
 * @author Ray
 * @date 2009-4-1
 */
public final class XlsProcessor {
 
 private static final XlsProcessor processor = new XlsProcessor();
 
 private XlsProcessor(){
 }
 
 public static XlsProcessor getInstance(){
  return processor;
 }
 
 public File convert(File cvsFile) throws IOException, RowsExceededException, WriteException{
  FileReader fileReader = new FileReader(cvsFile);
  BufferedReader bufferedReader = new BufferedReader(fileReader);
  String xlsFileName = getFileName(cvsFile.getName());
  File xls = new File(xlsFileName);
  WritableWorkbook workbook = Workbook.createWorkbook(xls);
  WritableSheet sheet = workbook.createSheet("CSV", 0);
  int rowNumber = 0;
  String row = bufferedReader.readLine();
  while(row != null){
   String[] cells = row.split(",");
   List cellList = format(cells);
   for(int i=0,size=cellList.size();i    sheet.addCell(new Label(i,rowNumber,cellList.get(i)));
   }
   rowNumber++;
   row = bufferedReader.readLine();
  }
  workbook.write();
  workbook.close();
  return xls;
 }
 
 public boolean compare(File a, File b) throws BiffException, IOException{
  Workbook aa = Workbook.getWorkbook(a);
  Workbook bb = Workbook.getWorkbook(b);
  Sheet aaSheet = aa.getSheet(0);
  Sheet bbSheet = bb.getSheet(0);
  int aaSheetRows = aaSheet.getRows();
  int bbSheetRows = bbSheet.getRows();
  int aaSheetCols = aaSheet.getColumns();
  int bbSheetCols = bbSheet.getColumns();
  boolean result = true;
  if(aaSheetRows == bbSheetRows && aaSheetCols == bbSheetCols){
   for(int i=0;i    for(int j=0;j     String aaCell = aaSheet.getCell(j, i).getContents();
     String bbCell = bbSheet.getCell(j, i).getContents();
     if(!aaCell.equals(bbCell)){
      System.out.println("Row:"+(i+1)+",Column:"+(j+1)+",one is "+aaCell+",the other is "+bbCell);
      result = false;
      break;
     }
    }
    if(result){
     continue;
    }else{
     break;
    }
   }
  }
  System.out.println("Compare result : "+result);
  return result;
 }
 
 private String getFileName(String csvFileName){
  return csvFileName.substring(0, csvFileName.length()-4)+".xls";
 }
 
 private List format(String[] cells){
  List cellList = new ArrayList();
  StringBuffer buffer = new StringBuffer();
  for(String cell : cells){
   boolean isHead = cell.startsWith("\"");
   boolean isTail = cell.endsWith("\"");
   if(isHead){
    if(isTail){
     cellList.add(cell.substring(1, cell.length()-1));
    }else{
     buffer.append(cell.substring(1)).append(',');
    }
   }else{
    if(isTail){
     buffer.append(cell.substring(0, cell.length()-1));
     cellList.add(buffer.toString());
     buffer.delete(0, buffer.length());
    }else if(buffer.length()>0){
     buffer.append(cell).append(',');
    }else{
     cellList.add(cell);
    }
   }
  }
  return cellList;
 }

Main函数

public static void main(String[] args) throws RowsExceededException, WriteException {
  try {
   XlsProcessor processor = XlsProcessor.getInstance();
   String csv = "CCDLPEA_15_2_15_100_1.CSV";
   File file = processor.convert(new File(csv));
   File referenceFile = new File("CCDLPEA_15_2_15_100_11.xls");
   boolean same = processor.compare(file, referenceFile);
   System.out.println(same);
  } catch (BiffException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }

 }

附件已测试通过~

实在有空写这个~呵呵

你可能感兴趣的:(CSV XLS Converter In Java (JXL Java Excel API))