我正在创建一个 java 程序来读取一个 excel 表并创建一个逗号分隔的文件。当我运行带有空白列的示例 excel 文件时,第一行运行良好,但其余行跳过空白单元格。我已经阅读了将空白单元格插入行所需的代码更改,但我的问题是为什么第一行有效????
public ArrayList OpenAndReadExcel(){
FileInputStream file = null;
HSSFWorkbook workBook = null;
ArrayList <String> rows = new ArrayList();
//open the file
try {
file = new FileInputStream(new File("Fruity.xls"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("Could not open Input File");
e.printStackTrace();
}
// open the input stream as a workbook
try {
workBook = new HSSFWorkbook(file);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Can't Open HSSF workbook");
e.printStackTrace();
}
// get the sheet
HSSFSheet sheet = workBook.getSheetAt(0);
// add an iterator for every row and column
Iterator<Row> rowIter = sheet.rowIterator();
while (rowIter.hasNext())
{
String rowHolder = "";
HSSFRow row = (HSSFRow) rowIter.next();
Iterator<Cell> cellIter = row.cellIterator();
Boolean first =true;
while ( cellIter.hasNext())
{
if (!first)
rowHolder = rowHolder + ",";
HSSFCell cell = (HSSFCell) cellIter.next();
rowHolder = rowHolder + cell.toString() ;
first = false;
}
rows.add(rowHolder);
}
return rows;
}
public void WriteOutput(ArrayList<String> rows) {
// TODO Auto-generated method stub
PrintStream outFile ;
try {
outFile = new PrintStream("fruity.txt");
for(String row : rows)
{
outFile.println(row);
}
outFile.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
-----
我在 .xls 文件中的输入(抱歉不知道如何在此处插入 Excel 表格)
名称 >>>>>>>>>> 原产国 >>>>>>>>> 国家产地>>>>>>> 等级>>>>>>
苹果月数>>>>>>>> 美国>>>>>>>>>>>>>>>>>>>>>> > 华盛顿 >>>>>>>>>>>>>> A >>>>>>>>> 6
橙 >>>>>> 美国 >>>>>>>>>>>>>>> >>>>>>>佛罗里达州>>>>>>>>>>>>>>>>一个>>>>>>>>> 9个
菠萝>>>>> 美国 >>>>>>>>>>>>>>>>>>>>>> 夏威夷 >>>>>>>>>>>>>>>>>> B >>>> >>>>> 10
草莓>>>>美国>>>>>>>>>>>>>>>>>>>>>>新泽西>>>>>>>>>>>>>> C >>>>>>>>>> 3
我的输出文本文件
Name ,Country of Origin,State of origin,,,,Grade,No of Months
Apple,USA,Washington,A,6.0
orange,USA,Florida,A, 9.0
菠萝,美国,夏威夷,B,10.0>>>> B >>>>>>>>> 10草莓>>>>美国>>>>>>>>>>>>>>>>>>>>>>新泽西>>>>> >>>>>>>>> C >>>>>>>>>> 3我的输出文本文件名称,原产国,原产国,,,等级,苹果,美国,华盛顿,A,月数, 6.0橙,USA,Florida,A,9.0菠萝,USA,Hawaii,B,10.0>>>> B >>>>>>>>> 10草莓>>>>美国>>>>>>>>>>>>>>>>>>>>>>新泽西>>>>> >>>>>>>>> C >>>>>>>>>> 3我的输出文本文件名称,原产国,原产国,,,等级,苹果,美国,华盛顿,A,月数, 6.0橙,USA,Florida,A,9.0菠萝,USA,Hawaii,B,10.03我的输出文本文件Name ,Country of Origin,State of origin,,,Grade,No of Months Apple,USA,Washington,A,6.0 orange,USA,Florida,A,9.0 pineapple,USA,Hawaii,B,10.03我的输出文本文件Name ,Country of Origin,State of origin,,,Grade,No of Months Apple,USA,Washington,A,6.0 orange,USA,Florida,A,9.0 pineapple,USA,Hawaii,B,10.0
草莓,美国,新泽西,C,3.0
Notice the two extra commas before the Grade column... This is because I have two blank columns there.<br/>
其余输出中缺少这些额外的逗号。
我正在使用 Apache Poi-3.9-20121203.jar