如何将管道符号分隔的txt文件转换为Java

时间:2018-04-12 13:24:09

标签: java excel csv

我需要将csv文件中的数据放入selenium中的excel中。

将csv文件格式化为:

PERIOD|EMPLID|EMPL_RCD|HOME HOST|NAME|FIRST_NAME|LAST_NAME|FTE|EMPL_STATUS
5/04/2018|78787|0|Home|mandon|steven|jabobs|1|A
6/04/2018|78789|0|Home|stacy|carvin|tans|1|A
11/04/2018|17892|0|Home|neel|harvis|bammer|1|A

需要在excel中拥有此数据,如图所示:

image

编辑我尝试创建Excel文件

我使用以下代码从csv文件生成带有管道符号分隔符的(.xls)文件,如图所示

image

但在阅读第一行后正在给java.lang.NullPointerException

public class DelimitedToXls {
    @SuppressWarnings("deprecation")
    public static void main(String args[]) throws IOException {
        ArrayList<ArrayList<String>> allRowAndColData = null;
        ArrayList<String> oneRowData = null;
        String fName = "C:\\input.csv";
        String currentLine;
        FileInputStream fis = new FileInputStream(fName);
        DataInputStream myInput = new DataInputStream(fis);
        int i = 0;
        allRowAndColData = new ArrayList<ArrayList<String>>();
        while ((currentLine = myInput.readLine()) != null) {
            oneRowData = new ArrayList<String>();
            String oneRowArray[] = currentLine.split(";");
            for (int j = 0; j < oneRowArray.length; j++) {
                oneRowData.add(oneRowArray[j]);
            }
            allRowAndColData.add(oneRowData);
            System.out.println();
            i++;
        }

     try {
         HSSFWorkbook workBook = new HSSFWorkbook();
         HSSFSheet sheet = workBook.createSheet("sheet1");
         for (int i = 0; i < allRowAndColData.size(); i++) {
           ArrayList<?> ardata = (ArrayList<?>) allRowAndColData.get(i);
           HSSFRow row = sheet.createRow((short) 0 + i);
           for (int k = 0; k < ardata.size(); k++) {
                System.out.print(ardata.get(k));
                HSSFCell cell = row.createCell((short) k);
                cell.setCellValue(ardata.get(k).toString());
           }
           System.out.println();
         }
       FileOutputStream fileOutputStream =  new FileOutputStream("C:\\outputFile.xls");
       workBook.write(fileOutputStream);
       fileOutputStream.close();
    } catch (Exception ex) {
   }
 }
}

3 个答案:

答案 0 :(得分:2)

您可以使用excel直接打开文件。打开文件 - &gt;选择文件类型:文本文件。 选择文件,然后在下一个窗口中选择“分隔”选项。下一个窗口选择'other'并输入|作为分隔符。 当然,将其保存为xls。

就是这样。

enter image description here

答案 1 :(得分:1)

您有3个主要选项:

  • 直接使用Excel打开它并将分隔符设置为|(管道)
  • 将其重写为有效的CSV(逗号分隔值)文件(即用逗号替换管道)
  • 将文件内容写入适当的Excel文件。
  

选项1 - 直接使用Excel打开

见Fabrizio的答案。

  

选项2 - 将其重写为有效的CSV文件

如果您确定文件中没有逗号

您只需要将所有|替换为,,以获得有效的csv(逗号分隔值)文件。然后你可以用Excel打开它。

String fileName = "/path/to/your/file/textFile.txt";
String csvFileName = "/path/to/your/file/csvFile.csv";

try (BufferedReader br = new BufferedReader(new FileReader(fileName));
     Writer writer = new FileWriter(csvFileName)) {
    String line;
    while ((line = br.readLine()) != null) {
        writer.append(line.replaceAll("[|]", ","));
        writer.append("\n");
    }
} catch(Exception e) {
    e.printStackTrace();
}

此代码将文件内容更改为

PERIOD,EMPLID,EMPL_RCD,HOME HOST,NAME,FIRST_NAME,LAST_NAME,FTE,EMPL_STATUS
5/04/2018,78787,0,Home,mandon,steven,jabobs,1,A
6/04/2018,78789,0,Home,stacy,carvin,tans,1,A
11/04/2018,17892,0,Home,neel,harvis,bammer,1,A

如果您的文件中可能有逗号

您需要通过令牌读取令牌,并使用双引号括起包含逗号的令牌。然后用逗号替换所有管道。例如,这一行

5/04/2018|78787|0|Home, Work|mandon|steven|jabobs|1|A

将转变为

5/04/2018,78787,0,"Home, Work",mandon,steven,jabobs,1,A

你可以这样做:

String fileName = "/path/to/your/file/textFile.txt";
String csvFileName = "/path/to/your/file/csvFile.csv";

try (BufferedReader br = new BufferedReader(new FileReader(fileName));
     Writer writer = new FileWriter(csvFileName)) {
    String line;
    while ((line = br.readLine()) != null) {
        String csvLine = Arrays.stream(line.split("[|]")) // split on pipes
            .map(token -> token.contains(",") ? "\""+token+"\"" : token) // surround with double quotes if there is a comma in the value
            .collect(Collectors.joining(",", "", "\n")); // join with commas
        writer.append(csvLine);
    }
} catch(Exception e) {
    e.printStackTrace();
}
  

选项3 - 写入Excel文件

您还可以使用Apache POI库创建正确的Excel文件.xls.xlsx。以下是使用POI-OOXML 3.17(截至今天的最新版本)You can get it from Maven Repository

的示例
String fileName = "/path/to/your/file/textFile.txt";
String excelFileName = "/path/to/your/file/excelFile.xlsx";

// Create a Workbook and a sheet in it
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Sheet1");

// Read your input file and make cells into the workbook
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
    String line;
    Row row;
    Cell cell;
    int rowIndex = 0;
    while ((line = br.readLine()) != null) {
        row = sheet.createRow(rowIndex);
        String[] tokens = line.split("[|]");
        for(int iToken = 0; iToken < tokens.length; iToken++) {
            cell = row.createCell(iToken);
            cell.setCellValue(tokens[iToken]);
        }
        rowIndex++;
    }
} catch(Exception e) {
    e.printStackTrace();
}

// Write your xlsx file
try (FileOutputStream outputStream = new FileOutputStream(excelFileName)) {
    workbook.write(outputStream);
    workbook.close();
} catch (IOException e) {
    e.printStackTrace();
}

答案 2 :(得分:1)

您可以将|替换为char \t并将其存储在.csv文件中。 将CSV转换为XLS时,我做过类似的事情:

try{
            FileReader fr=new FileReader("TEJAS.CSV");
            FileWriter fw=new FileWriter("TEJASEXEL.xls");
            while((c=fr.read())!=-1){
                if(c==','){
                    c='\t';
                }
                fw.write(c);
            }
            fr.close();
            fw.close();

        }
相关问题