将字符串转换为xls - Java

时间:2015-07-31 03:20:44

标签: java excel apache-poi

好吧,我的项目中有一个String,就像这样:

,0,0,0,0,0,1,1,
,0,0,0,7,8,6,6,
,3,3,3,3,9,4,5,
,5,6,6,9,5,2,1,
,6,2,8,0,0,3,9,
--------------------------------------------------
Reference,-,C,A,A,G,A,T,
17-F1,.,.,.,.,.,T,C,
37-F2,1A,A,C,T,T,.,.,

我需要将其转换为XLS文件

1 个答案:

答案 0 :(得分:2)

我的awnser使用Apache poi和Commons IO

Workbook wb = new HSSFWorkbook();
CreationHelper helper = wb.getCreationHelper();
Sheet sheet = wb.createSheet("new sheet");

List<String> lines = IOUtils.readLines(new StringReader(csv));

    for (int i = 0; i < lines.size(); i++) {
        String str[] = lines.get(i).split(",");
        Row row = sheet.createRow((short) i);
        for (int j = 0; j < str.length; j++) {
             row.createCell(j).setCellValue(helper.createRichTextString(str[j]));

                 }
              }

FileOutputStream fileOut = new FileOutputStream("c:\\someName.xls");
    wb.write(fileOut);
    fileOut.close();