我是Apache-poi的新手,並且使用3.8版的poi。在Excel中寫入值時,如果它們匹配,我檢查列名,我會在該列中寫入一些值並完成它,然後我將開始在頭上寫入。問題是,如果只寫入三個列值,最後一列值是添加或保存的,並且前兩個值不保存在列中。任何人都可以告訴哪裏出了錯。 (不好意思,萬一有什麼錯誤)Excel在編寫時缺少一些值
Code:
int i = 0;
Row row = sheet.createRow(i);
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFillBackgroundColor(HSSFColor.LIGHT_ORANGE.index);
String validate_header = null;
while (iterator.hasNext()) {
if (eiterator.hasNext()) {
validate_header = eiterator.next();
}
Cell cell = row.createCell(i);
String col_heading = iterator.next();
cell.setCellValue(col_heading);
if(col_heading.equalsIgnoreCase("Assigned Date"))
{
Add_value(i, col_heading, row, sheet);
row=sheet.getRow(0);
cell=row.getCell(i);
}
else if(col_heading.startsWith("Review"))
{
int count=-1;
int n=Col_values.get("Defect Summary").size();
for (int j = 0; j < n; j++) {
row = sheet.createRow(count);
cell = row.createCell(i);
String s="External QC Defect ";
cell.setCellValue(s);
count++;
}
row=sheet.getRow(0);
cell=row.getCell(i);
}
sheet.autoSizeColumn(i);
i++;
}
private static Sheet Add_value(int k,String name,Row row, Sheet sheet) {
System.out.println("Inside the add method");
if(name.equalsIgnoreCase("Assigned Date")||name.equalsIgnoreCase("Reported Date"))
{
vector = Col_values.get("TargetDate");
int count = 1;
System.out.println("IF Size of the vector " + vector.size());
for (int j = 0; j < vector.size(); j++) {
row = sheet.createRow(count);
cell = row.createCell(k);
String s = (String) vector.get(j);
System.out.println(s);
cell.setCellValue(s);
count++;
}
}
else
{
vector = Col_values.get("Defect Summary");
int count = 1;
System.out.println("ELSE Size of the vector " + vector.size());
for (int j = 0; j < vector.size(); j++) {
row = sheet.createRow(count);
cell = row.createCell(k);
String s = (String) vector.get(j);
System.out.println(s);
cell.setCellValue(s);
count++;
}
}
return sheet;
}
'
u能告訴了什麼問題?
哪裏是代碼?我們如何知道你錯在哪裏? – 2012-02-11 04:18:41
對不起。我現在添加了。 – Loga 2012-02-11 05:06:52
從頭開始重寫代碼:它有太多的開放端(迭代器)和太多的本地目的(使用createRow)。遵循Java約定(即在這裏接受)也會更好:更多地使用空格,'camelCaseNames' i.o. 'Capitalized_underscored_ones'。您可以將小功能的自頂向下設計用於特定目的。 _我瞭解原因:首先用醜陋的POI進行實驗._ – 2012-02-11 09:48:47