Java 使用ApachePOI将数据按列导出到excel工作表?
我设法将数据写入一列。 我的要求是将数据写入下一个相邻列,而不重写第一列中的数据。 有什么帮助吗Java 使用ApachePOI将数据按列导出到excel工作表?,java,excel,apache-poi,Java,Excel,Apache Poi,我设法将数据写入一列。 我的要求是将数据写入下一个相邻列,而不重写第一列中的数据。 有什么帮助吗 InputStream inp; try { inp = new FileInputStream("D:\\test.xls"); Workbook wb = WorkbookFactory.create(inp); Sheet sheet = wb.getSheetAt(0); Row row = null;
InputStream inp;
try {
inp = new FileInputStream("D:\\test.xls");
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
Row row = null;
Cell c = null;
int rowNum=0;
Set<String> keySet = new HashSet<String>();
keySet = tushMap.keySet();
for (Entry<String, String> entry : tushMap.entrySet()) {
row=sheet.createRow((short) (sheet.getLastRowNum() + 1));
System.out.println((short) (sheet.getLastRowNum() + 1));
c = row.createCell(0);
c.setCellValue("fff");
System.out.println("fff");
System.out.println(entry.getKey() + "/" + entry.getValue());
rowNum++;
}
FileOutputStream fileOut = new FileOutputStream("D:\\test.xls");
wb.write(fileOut);
fileOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
InputStream-inp;
试一试{
inp=新文件inputstream(“D:\\test.xls”);
工作簿wb=WorkbookFactory.create(inp);
Sheet Sheet=wb.getSheetAt(0);
行=空;
细胞c=null;
int rowNum=0;
Set keySet=newhashset();
keySet=tushMap.keySet();
for(条目:tushMap.entrySet()){
行=sheet.createRow((短)(sheet.getLastRowNum()+1));
System.out.println((短)(sheet.getLastRowNum()+1));
c=行。创建单元(0);
c、 setCellValue(“fff”);
系统输出打印项次(“fff”);
System.out.println(entry.getKey()+“/”+entry.getValue());
rowNum++;
}
FileOutputStream fileOut=新的FileOutputStream(“D:\\test.xls”);
wb.写入(文件输出);
fileOut.close();
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(无效格式){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
这是为填充第1列而编写的代码。
填充第二列将覆盖第一列1。。
请提出一些解决办法。。
提前感谢。要并排获取数据,请获取循环外的最后一行,并将其另存为起始行。在循环中为正在编辑的行保留一个计数器。如果该行为空,则创建该行
rowPos = sheet.getLastRowNum();
for (Entry<String, String> entry : tushMap.entrySet()) {
rowPos++;
Row currentRow = sheet.getRow(rowPos);
if(null == currentRow)
currentRow = createRow(rowPos);
这样做了..还将sheet.getLastRowNum()+1更改为sheet.getFirstRowNum()+1(+1,以便跳过列标题)谢谢你的帮助:)
int beginingRow = sheet.getLastRowNum();
for( int col=0; col<2; col++){
int currentRow = beginingRow;
for (int i=0; i<10; i++) {
currentRow++;
row = sheet.getRow(currentRow);
if(null == row)
row=sheet.createRow(currentRow);
c = row.createCell(col);
c.setCellValue("fff");
System.out.println("fff");
//System.out.println(entry.getKey() + "/" + entry.getValue());
rowNum++;
}
}
fff fff
fff fff
fff fff
fff fff
fff fff
fff fff
fff fff
fff fff
fff fff
fff fff