1

I am working on a project where i have to read a string from excel file and write it in to a csv file along with the sequence.

Here is my code:

private void readExcelSheet(String inputFile){
//Method to read the input file 
    File excelSheet = new File(inputFile);             
    set1=new LinkedHashSet<String>();
    InputStream inp = null;
    try {
        inp = new FileInputStream(excelSheet);
        wb =WorkbookFactory.create(inp);
        for(int sheetNo=0; sheetNo<wb.getNumberOfSheets();sheetNo++)
        { //counter5++;
            System.out.println(sheetNo);
            map1=new LinkedHashMap<String,ArrayList<String>>();
            Sheet sheet = wb.getSheetAt(sheetNo);
            Iterator<Row> rowIterator = sheet.iterator(); 
            while(rowIterator.hasNext()) {
                Row row = rowIterator.next();
                //array1=new ArrayList<String>();
                temp=row.getCell(0).getStringCellValue();
                System.out.println(temp);
                inputInto(temp);
            }
        }
    }catch (Exception ex) {
        ex.printStackTrace();
    }
}

public void inputInto(String temp1) throws IOException{
    FileWriter writer = new FileWriter(fileName,true);
    PrintWriter out = new PrintWriter(writer);
    out.append("\""+temp+"\"");
    out.append(",");
    out.append((countLines()+1)+"");
    System.out.println(countLines()+1);
    out.append("\n");
    out.flush();
}

public int countLines() throws IOException {
    LineNumberReader reader  = new LineNumberReader(new FileReader(fileName));
    int cnt = 0;
    String lineRead = "";
    while ((lineRead = reader.readLine()) != null) {}
    cnt = reader.getLineNumber(); 
    reader.mark(0);
    reader.reset();
    reader.close();
    return cnt;
}

public static void main(String[] args){
    Token token=new Token();
    token.readExcelSheet("D:/Workspace/sample.xlsx");
}

Here readExcelSheet is the method to read the input excel sheet
inputInto is the method to write in to csv file
countLines is the method to find the last line number, so as to find the sequence.

but when i try to run my code for the first time i get the sequence as '1' and when i run it for the second time i am getting the sequence as '11' and 23 and soon.

Here is my input:

"Horizon Dental Option Plans offer the freedom to receive dental services from any dentist, while enabling members to save money and maximize benefits by choosing dentists within our large nationwide network.
Horizon Dental Option Plans offer: • The flexibility of three benefit levels • Lower out-of-pocket costs and higher plan benefits when using dentists who participate in the Horizon Dental Option Plan • Access to a nationwide network of more than 136,000 participating dentists • Discounts between 10 and 30 percent with in-network dentists • Preventive dental services covered 100% of allowed charges • Annual deductible options • The option to go out-of-network; deductibles and coinsurance may apply For more information, download the plan details.
For more information, download the plan details."

Can anyone please help me with this. Thanks..

4

1 回答 1

0

我认为你的错误是

FileWriter writer = new FileWriter(fileName,true);

当您说true,在第二次调用中没有删除输出文件时,java 会在其上写入。只需删除true该行,我猜它就会起作用。

于 2013-04-29T09:45:12.687 回答