csv java

COMMA SEPARATED VALUE的缩写,出现在档案总管中的档案类型是「逗号分格」,依计算机原来的设定,如果直接点选该档案,计算机将以EXCEL的模式开启该档案。但建议您千万不要这么做,因为CSV档如果以EXCEL开启,由于计算机档案数据转换的原因,会将其CRC之数值改做科学记号方式储存,而造成档案中的 CRC值发生错误。这也是笔者初出茅庐时所得到的惨痛经验,但如果想一窥CSV档的真实面貌,笔者建议您使用WORDPAD或是记事本(NOTE)来开启,再则先另存新档后用EXCEL开启,也是方法之一。
开启后的CSV档包含了四或五个字段(部分),由左至右分别记载着:文件名称(XXXX.JPG)、档案大小(以BYTE为单位)、CRC32值(八个英文字母或数字组成)、
档案路径(档案储存的子目录名称)和档案内容描述(一般来说都是明星的英文姓名甚或是专辑名称,也可能空白)。而其中第四栏「档案路径」因为每个人储存整理图档的方式不同,所以本栏通常不存在,而一般有含有「档案路径」这栏的CSV档,又称为ECSV档案 CSV文件,也叫逗号分隔值文件,英文名称COMMA SEPARATED VALUE

具体文件格式

每条记录占一行
以逗号为分隔符
逗号前后的空格会被忽略
字段中包含有逗号,该字段必须用双引号括起来
字段中包含有换行符,该字段必须用双引号括起来
字段前后包含有空格,该字段必须用双引号括起来
字段中的双引号用两个双引号表示
字段中如果有双引号,该字段必须用双引号括起来
第一条记录,可以是字段名



我们经常将Excel格式的文件保存为csv格式以方便上传和修改,可是当数据中包含逗号和双引号的时候Excel会把该字段用双引号括住并把数据中的"改为"",从而给解析带来了困难。我写了以下函数来解析这样的字符串:

testSplitCSV.java:

import java.util.Vector;
class  testSplitCSV{
/**
* Split one line of csv file
* @return a String array results
*/
public static String[] splitCSV(String src) throws Exception{
  if (src==null || src.equals("")) return new String[0];
  StringBuffer st=new StringBuffer();
  Vector result=new Vector();
  boolean beginWithQuote = false;
  for (int i=0;i<src.length();i++){
   char ch = src.charAt(i);
   if (ch=='\"'){
    if (beginWithQuote){
     i++;
     if (i>=src.length()){
      result.addElement(st.toString());
      st=new StringBuffer();
      beginWithQuote=false;
     }else{
      ch=src.charAt(i);
      if (ch == '\"'){
       st.append(ch);
      }else if (ch == ','){
       result.addElement(st.toString());
       st=new StringBuffer();
       beginWithQuote = false;
      }else{
       throw new Exception("Single double-quote char mustn't exist in filed "+(result.size()+1)+" while it is begined with quote\nchar at:"+i);
      }
     }
    }else if (st.length()==0){
     beginWithQuote = true;
    }else{
     throw new Exception("Quote cannot exist in a filed which doesn't begin with quote!\nfield:"+(result.size()+1));
    }
   }else if (ch==','){
    if (beginWithQuote){
     st.append(ch);
    }else{
     result.addElement(st.toString());
     st=new StringBuffer();
     beginWithQuote = false;
    }
   }else{
    st.append(ch);
   }
  }
  if (st.length()!=0){
   if (beginWithQuote){
    throw new Exception("last field is begin with but not end with double quote");
   }else{
    result.addElement(st.toString());
   }
  }
  String rs[] = new String[result.size()];
  for (int i=0;i<rs.length;i++){
   rs[i]=(String)result.elementAt(i);
  }
return rs;
}

public static void main(String[] args){
  String src1=  "\"fh,zg\",sdf,\"asfs,\",\",dsdf\",\"aadf\"\"\",\"\"\"hdfg\",\"fgh\"\"dgnh\",hgfg'dfh,\"asdfa\"\"\"\"\",\"\"\"\"\"fgjhg\",\"gfhg\"\"\"\"hb\"";
  try {
   String[] Ret = splitCSV(src1);
   for (int i=0;i<Ret.length;i++){
    System.out.println(i+": "+Ret[i]);
   }
  }
  catch(Exception e) {
   e.printStackTrace();
  }
}
}


/**
* 在很多时候我们都需要将一些数据集合以某种文件格式输出,其中CSV文件输出是一种比较常用的方式.
* 下面是一个简单的实现CSV文件输出的代码,与大家共享.
*/
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import org.apache.struts.actions.DispatchAction;
import java.io.PrintWriter;
import javax.servlet.http.HttpSession;


public class CsvUtil extends DispatchAction {

public ActionForward exportCsv(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws Exception {

   HttpSession session = request.getSession();
   //头部
   String data = "姓名,电子邮件地址,移动电话,业务电话,业务传真,职务,部门\n";
   //具体的数据,可以动态的从数据库中读取
   data += "张三,[email protected],13800000000,010-88889999,010-99998888,部门领导,产品研发部\n";
   data += "李四,[email protected],13800000001,010-88889999,010-99998888,业务助理,产品研发部\n";
   StringBuilder str = new StringBuilder();
   str.append(data);
   response.setContentType("text/csv");
   response.setCharacterEncoding("gb18030");
   response
     .setHeader("Content-Disposition",
       "attachment; filename=\""
         + new String("通讯薄.csv".getBytes("gb18030"),
           "iso8859-1") + "\"");
   PrintWriter out = response.getWriter();
   out.write(str.toString());
   out.flush();
   out.close();
   return null;
}

}




import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.util.List;

import au.com.bytecode.opencsv.CSVReader;
import au.com.bytecode.opencsv.CSVWriter;
import au.com.bytecode.opencsv.bean.ColumnPositionMappingStrategy;
import au.com.bytecode.opencsv.bean.CsvToBean;

public class CsvDemo {
   
    public static void main(String[] args) throws IOException {
       
        final String[] header = new String[]{"name", "sex", "age"};
        final String[][] data = new String[][]{header, {"Lucy", "F", "22"}, {"Tom", "M", "25"}, {"Lily", "F", "19"}};

         File tempFile = File.createTempFile("csvWriterTest", ".csv");
         tempFile.deleteOnExit();

         CSVWriter writer = new CSVWriter(new FileWriter(tempFile));
        for (int i = 0; i < data.length; i++) {
             writer.writeNext(data[i]);
         }
         writer.close();

         CSVReader reader = new CSVReader(new FileReader(tempFile));
         String[] line;
         String readline = "";
        for (int row = 0; (line = reader.readNext()) != null; row++) {
            for (int col = 0; col < line.length; col++) {
                if (data[row][col] != null) {
                     System.out.print(line[col] + (col != line.length - 1 ? "," : ""));
                     readline = readline + line[col] + (col != line.length - 1 ? "," : " ");
                 }
             }
             System.out.println("");
         }
       
         ColumnPositionMappingStrategy strat = new ColumnPositionMappingStrategy();
         strat.setType(Person.class);
         String[] columns = new String[] {"name", "sex", "age"};
         strat.setColumnMapping(columns);
         CsvToBean csv = new CsvToBean();
         List<?> persons = csv.parse(strat, new StringReader(readline));
        for (int i = 1; i < persons.size(); i++) {
             System.out.println(((Person)persons.get(i)).getName());
             System.out.println(((Person)persons.get(i)).getSex());
             System.out.println(((Person)persons.get(i)).getAge());
         }
       
         reader.close();
     }
}

你可能感兴趣的:(java,Excel)