【问题标题】:converting csv to xls in java taking care of a column在java中将csv转换为xls来处理一列
【发布时间】:2016-02-09 16:45:35
【问题描述】:

我是一名 java 开发人员,我想将 .csv 文件转换为 .xls 2003 格式,以便将我的 csv 转换为 .xls 文件,我的 .csv 文件的结构将类似于

REC_STATUS,TRADE_ID,SETTLEMENT_DATE,TRADE_EFFECTIVE_DATE,PAYMENT_TYPE,VERSION,BREAK_DOWN_BUCKET,CAUSE,NUM_CASHFLOWS_AFFECTED,PROFILE
Found only in File :B,178942690,01-Feb-16,03-Dec-14,"Coupon",5,NOISY_BREAK_BUCKET,REC_TOOL_ISSUE_PAYMENT_DIRECTION_MISMATCH | REC_TOOL_ISSUE_NOTIONAL_MISMATCH | TRADE_VERSION,1,AVS Offshore
Found only in File :B,197728700,Various,21-Dec-15,"Coupon,(x20)",2,ACTUAL DATA BREAK BUCKET,ACTUAL_DATA_BREAK,20,AVS Offshore

现在您可以看到 .csv 文件是用逗号分隔的,但是对于付款类型下的值,该值类似于 "Coupon,(x20)" 现在,该值应被视为单个值,因此逻辑应该与第五列的逻辑相同索引将从 0 开始声明,因此列的位置在 .csv 文件中是固定的,因此对于第五列付款类型的值,如果值为 "Coupon,(x20)",则应将其视为单个不拆分此第五列值所以对于第五列值,逻辑应该从双引号开始并以双引号结束,所以请告知我将如何将 .csv 转换为 .xls 并同时处理第五列值

我尝试过的是,如下所示,请告知我如何更正此代码并克服问题

public class CSVToExcelConverter {

public static void main(String args[]) throws IOException
{
ArrayList arList=null;
ArrayList al=null;
String fName = "test.csv";
String thisLine;
int count=0;
FileInputStream fis = new FileInputStream(fName);
DataInputStream myInput = new DataInputStream(fis);
int i=0;
arList = new ArrayList();
while ((thisLine = myInput.readLine()) != null)
{
al = new ArrayList();
String strar[] = thisLine.split(",");
for(int j=0;j<strar.length;j++)
{
al.add(strar[j]);
}
arList.add(al);
System.out.println();
i++;
}

try
{
HSSFWorkbook hwb = new HSSFWorkbook();
HSSFSheet sheet = hwb.createSheet("new sheet");
for(int k=0;k<arList.size();k++)
{
ArrayList ardata = (ArrayList)arList.get(k);
HSSFRow row = sheet.createRow((short) 0+k);
for(int p=0;p<ardata.size();p++)
{
HSSFCell cell = row.createCell((short) p);
String data = ardata.get(p).toString();
if(data.startsWith("=")){
cell.setCellType(Cell.CELL_TYPE_STRING);
data=data.replaceAll("\"", "");
data=data.replaceAll("=", "");
cell.setCellValue(data);
}else if(data.startsWith("\"")){
data=data.replaceAll("\"", "");
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue(data);
}else{
data=data.replaceAll("\"", "");
cell.setCellType(Cell.CELL_TYPE_NUMERIC);
cell.setCellValue(data);
}
//*/
// cell.setCellValue(ardata.get(p).toString());
}
System.out.println();
}
FileOutputStream fileOut = new FileOutputStream("test.xls");
hwb.write(fileOut);
fileOut.close();
System.out.println("Your excel file has been generated");
} catch ( Exception ex ) {
ex.printStackTrace();
} //main method ends
}
}

【问题讨论】:

  • 对列使用计数器。遇到第5列,直接读两列,跳到第7列
  • @NikithaReddy 非常感谢,你能不能也请展示一下,如果你能发布代码我会调试它,以便提前掌握谢谢
  • @SheetalMohanSharma 我已经编辑并添加了代码,请检查
  • 您可能想要实现或使用正确的CSV parser,它不会在带引号的字符串中搜索分隔符。

标签: java


【解决方案1】:

您可以使用opencsv

 CSVReader reader = new CSVReader(
                new FileReader("c:/xxx.csv"), ',', '"', 1);
 // Read all rows at once
 List<String[]> allRows = reader.readAll();

此代码忽略引号中的分隔符 - , - " 并跳过第一行 (REC_STATUS,TRADE_ID,SETTLEMENT_DATE ...)。

如果您不需要跳过第一行,只需使用默认值(分隔符 - , 引号 - "

CSVReader reader = new CSVReader(
                    new FileReader("c:/xxx.csv"));
// Read all rows at once
List<String[]> allRows = reader.readAll();

【讨论】:

  • 请告知它会跳过第一行的上下文到我给出的 .csv 文件的含义
  • 可能会跳过标题行
  • @v.ladynev 非常感谢,但您能否告知一下我将如何生成 .xls 工作簿,因为我正在使用 poi 3.10 版本,现在我拥有我想要的 allRows 数组中的所有数据以类似的方式简单地将其放入 .xls excel 文件中
  • @v.ladynev 所以我也请告知我想读取所有行以及双引号值作为单个所以最后的语句将是 CSVReader reader = new CSVReader( new FileReader("c:/ xxx.csv"), ',', '"');
  • @sss 是 CSVReader reader = new CSVReader( new FileReader("c:/xxx.csv"), ',', '"');,但它与 CSVReader reader = new CSVReader( new FileReader("c:/xxx.csv")); 相同,因为这是 opencsv 的默认行为。
【解决方案2】:
    public class CSVToExcelConverter {

    public static void main(String args[]) throws IOException
    {
         ArrayList arList=null;
         ArrayList al=null;
         String fName = "test.csv";
         String thisLine;
         int count=0;
         FileInputStream fis = new FileInputStream(fName);
         DataInputStream myInput = new DataInputStream(fis);
         int i=0;
         arList = new ArrayList();
         while ((thisLine = myInput.readLine()) != null)
         {
              al = new ArrayList();
              String strar[] = thisLine.split(",");
              for(int j=0;j<strar.length;j++)
              {
                  if(j == 5){
                       a1.add(strar[j] + "," + strar[j+1]);
                       j++;
                  }
                  al.add(strar[j]);
              }
              arList.add(al);
              i++;
         }
    }
}

【讨论】:

  • 谢谢,但请为我的上述解决方案提供建议,因为我需要在上述解决方案中实现逻辑
  • 你能解释一下上面这段代码发生了什么,还请告知输出文件的位置在哪里提到
  • 当遇到第 5 列时,会考虑第 5 列和第 6 列,并在其间添加“,”。顺便说一句,我刚刚复制了您的部分代码。
  • 非常感谢让我试试看
  • 你不要关闭流,不要指定编码(像我一样:)),并使用已弃用的DataInputStream.readLine()——用它来读取行是很奇怪的。
猜你喜欢
  • 1970-01-01
  • 2014-06-05
  • 1970-01-01
  • 2011-11-02
  • 2011-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多