【发布时间】:2015-03-19 05:25:05
【问题描述】:
我的要求是使用 Apache POI 将 HashMap 写入 excel,hashmap 中的键是我的单元格 ID,hashmap 中的值是要设置到该单元格中的值。 以下是我的代码:
public static final String[] ExcelColumns = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
"P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA", "AB", "AC", "AD", "AE", "AF", "AG", "AH", "AI",
"AJ", "AK", "AL", "AM", "AN", "AO", "AP", "AQ", "AR", "AS", "AT", "AU", "AV", "AW", "AX", "AY", "AZ", "BA",
"BB", "BC", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BK", "BL", "BM", "BN", "BO", "BP", "BQ", "BR", "BS",
"BT", "BU", "BV", "BW", "BX", "BY", "BZ", "CA", "CB", "CC", "CD", "CE", "CF", "CG", "CH", "CI", "CJ", "CK", "CL", "CM",
"CN", "CO", "CP", "CQ", "CR", "CS", "CT", "CU", "CV", "CW", "CX", "CY", "CZ", "DA", "DB", "DC", "DD", "DE", "DF", "DG",
"DH", "DI", "DJ", "DK", "DL", "DM", "DN", "DO", "DP", "DQ", "DR", "DS", "DT", "DU", "DV", "DW", "DX", "DY", "DZ"};
public static int row(Map.Entry pair) {
//System.out.println("The row is:"+k);
return new Integer(pair.getKey().toString().substring(1));
}
public static int column(Map.Entry pair) {
String indexVal = pair.getKey().toString().substring(0, 1);
String[] ExcelColumns = CompareExcelDAO.ExcelColumns;
int index = 0;
for (int i = 0; i < ExcelColumns.length; ++i) {
if (indexVal.equals(ExcelColumns[i])) {
index = i;
}
}
return index+1;
}
public void CopyContentsOfExcel(HSSFWorkbook workbook, HSSFSheet sheet,
String ExcelPath_1, int i, HashMap getMismatchMap)
throws IOException {
int rownum = sheet.getLastRowNum()+2; // to check last used row in Excel
//CompareExcelDAO mapDao = new CompareExcelDAO();
Iterator it1 = getMismatchMap.entrySet().iterator();
while (it1.hasNext()) {
Map.Entry pair = (Map.Entry) it1.next();
int r = row(pair) + rownum;
int c= column(pair);
System.out.println("c = "+c+" r = "+r);
System.out.println("Key: "+pair.getKey()+" "+"Value: "+pair.getValue());
sheet.createRow(r).createCell(c).setCellValue(pair.getValue().toString());
}}
问题是它只将一对键/值打印到两个固定单元格中,在调试模式下,所有行/列值都按预期显示,但是当我打开工作表时只打印一个值。 http://www.tagwith.com/question_1103031_printing-a-hashmap-to-excel-using-jxl-api,该站点提供了一种使用 jxl 将 Hashmap 写入 Excel 的方法,但是我的所有项目都使用 Apache POI。
【问题讨论】:
-
我想比较两个excel,把区别写在第三张excel表中
标签: java hashmap apache-poi export-to-excel