代码之家  ›  专栏  ›  技术社区  ›  Gaurav Thantry

无法使用多个工作簿将值写入Excel。写入方法

  •  0
  • Gaurav Thantry  · 技术社区  · 6 年前

    如果我调用 owb.write(fileOut) fileOut.close() 方法只在结尾处出现一次(注释为 write and close positioning )但这里的问题是当 k=1 ,未在工作簿中打印。当迭代在其他列和 K=1 。只有第一个迭代没有打印。其余的值设置正确。

    我尝试使用多重 workbook.write() 方法。如果你看下面的代码,注释如下 [1] ,我不得不调用 owb.write(文件输出) 分别在 if 条件(注释为 if condition[1] ) else 条件(注释为 else condition [2] )因为正如我所说,第一个值没有在工作簿中设置。尝试在此方案中执行代码时出现以下运行时错误:

    保存失败:保存包时出错:无法将part/docprops/app.xml与marshaller org.apache.poi.openxml4j.opc.internal.marshaller.defaultmarshaller@3740f768一起保存到流中

           for(int i=0;i<noOfCols1;i++)
            {
            	for(int j=1;j<=noOfRows1;j++)
            	{
            		value1 = formatter.formatCellValue(sheet1.getRow(j).getCell(i));
            			for(int m=1;m<=noOfRows2;m++)
            			{
            				value2 = formatter.formatCellValue(sheet2.getRow(m).getCell(i));
            				value1= value1.trim();
            				value2=value2.trim();
            				int value2Position = sheet2.getRow(m).getCell(i).getRowIndex();
            				if(!positions.contains(value2Position))
            				{
            				 if(value1.contentEquals(value2))
            				 {
            					positions.add(value2Position);
            					matched = true;
            				 }
            				 else{
            					 matched = false;
            				 }
            				}
            				if(matched==true)
            				{
            					break;
            				}
            			}
            			if(matched == false)
            			{   
            				int k=1;
            				if(cFilledPositions.isEmpty())   //If condition[i]
            				{
            					rowHead =  sheet.createRow((short)k);
                				rowHead.createCell(i).setCellValue(value1);
                				owb.write(fileOut);   //[1]
            				}
            				else     //else condition [1]
            				{
            					int l = cFilledPositions.size()-1; 
            						k = cFilledPositions.get(l)+1;
            						rowHead =  sheet.createRow((short)k);
                    				rowHead.createCell(i).setCellValue(value1);
                    				owb.write(fileOut);
            				}
            				cFilledPositions.add(k);	
                 		}
            			matched = false;
            	}
            	cFilledPositions.clear();
            	positions.clear(); 
            }
                   //write and close positioning
            fileOut.close();
    1 回复  |  直到 6 年前
        1
  •  0
  •   Gaurav Thantry    6 年前

    我尝试调试,发现createRow()方法会删除先前在同一行上再次调用时创建的值。

    为了详细说明这一点,假设sheet.createRow()在第一次迭代中设置单元格的值,并且当它在 j for 循环 cFilledPositions 列表被清除,当它在进入主循环后返回时,'cfilledpositions' will be empty and the integer K will again be initialized to . This is when createRow(k)`再次调用1。这将清除第一行中以前存在的值。我正在尝试解决这个问题,如果我的代码有效的话,我会用解决方案编辑我的答案。

    下面是我们的工作。我检查了这行是否是空的。仅当行为空时才调用CreateRow函数。我已经为新代码添加了注释。

     for(int i=0;i<noOfCols1;i++)
            {
            	for(int j=1;j<=noOfRows1;j++)
            	{
            		value1 = formatter.formatCellValue(sheet1.getRow(j).getCell(i));
            			for(int m=1;m<=noOfRows2;m++)
            			{
            				value2 = formatter.formatCellValue(sheet2.getRow(m).getCell(i));
            				value1= value1.trim();
            				value2=value2.trim();
            				int value2Position = sheet2.getRow(m).getCell(i).getRowIndex();
            				if(!positions.contains(value2Position))
            				{
            				 if(value1.contentEquals(value2))
            				 {
            					positions.add(value2Position);
            					matched = true;
            				 }
            				 else{
            					 matched = false;
            				 }
            				}
            				if(matched==true)
            				{
            					break;
            				}
            			}
            			if(matched == false)
            			{   
            				int k=1;
                            
            				if(cFilledPositions.isEmpty())
            				{
            					try{
            					isEmpty = checkIfRowIsEmpty(sheet,k,formatter);
            					if(isEmpty)
            					{
            						rowHead = sheet.createRow(k);
            					}
                               rowHead.createCell(i).setCellValue(value1);
            					}
            					catch (Exception e){
            						try{
            							rowHead = sheet.createRow(k);
            							 rowHead.createCell(i).setCellValue(value1);
            						}
            						catch (Exception e1){
            							
            						}
            					}
                				
            				}
            				else
            				{
            					int l = cFilledPositions.size()-1;
            						k = cFilledPositions.get(l)+1;
            						try{
                    					isEmpty = checkIfRowIsEmpty(sheet,k,formatter);
                    					if(isEmpty)
                    					{
                    						rowHead =  sheet.createRow(k);
                    					}
                    				rowHead.createCell(i).setCellValue(value1);
            						}
            						catch (Exception e)
            						{
            							try{
                							rowHead = sheet.createRow(k);
                							 rowHead.createCell(i).setCellValue(value1);
                						}
                						catch (Exception e1){
                							
                						}
            						}
            				}
            				cFilledPositions.add(k);	
                 		}
            			matched = false;
            	}
            	cFilledPositions.clear();
            	positions.clear(); 
            }