从/xl/worksheets/sheet1.xml part coldfusion poi中删除记录合并单元格

时间:2016-01-22 10:08:15

标签: excel coldfusion apache-poi

我正在使用java poi库创建一个excel文件,它会被创建但是当我打开文件时,excel弹出一条消息说 “我们发现文件内容存在一些问题。您是否希望我们尽可能多地恢复。如果您信任工作簿的来源。”

单击是后,它会弹出“修复的部件:/xl/worksheets/sheet1.xml部件出现XML错误。加载错误。第121行,第0列。 删除记录:合并来自/xl/worksheets/sheet1.xml部分的单元格。

文件中没有数据丢失,我无法理解excel错误背后的原因。

我不能在这里编写完整的代码,但这里是与xlsx文件相关的主要内容:

<cfdirectory action="list" directory="#expandPath('/poi_39')#" name="poidir"/>
            <cfset paths = arrayNew(1)>
            <cfloop query="poidir">
                <cfset arrayAppend(paths, directory & "\" & name) />
            </cfloop>
            <cfset server.loader = createObject("component", "javaloader.JavaLoader").init(loadPaths=paths, loadColdFusionClassPath=true) />
            <cfscript>
                _Thread = createObject("java", "java.lang.Thread");
                currentClassloader = _Thread.currentThread().getContextClassLoader();
                try {
                    // Set the current thread's context class loader as Javaloader's classloader, so dom4j doesn't die
                    _Thread.currentThread().setContextClassLoader(server.loader.getURLClassLoader());
                    //code found online
                    //inp = createObject("java", "java.io.FileOutputStream").init("#fullpath#");
                    //reseting the value on code
                    inp = 100;
                    //workBook = server.loader.create("org.apache.poi.xssf.usermodel.XSSFWorkbook").Init(inp);
                    workBook = server.loader.create("org.apache.poi.xssf.streaming.SXSSFWorkbook").Init(inp);
                }
                catch(Any exc) {
                    rethrow;
                }
                finally { // We have to reset the classloader, due to thread pooling.
                    _Thread.currentThread().setContextClassLoader(currentClassloader);
                }
            </cfscript>

            <!--- New code ends--->
            <cfset cellstyle = workbook.createCellStyle()>
            <cfset fontface = workbook.createFont()>
            <cfset fontface.setBoldweight(fontface.BOLDWEIGHT_BOLD)>
            <cfset cellstyle.setFont(fontface)>
            <cfset cellstyleData = workbook.createCellStyle()>
            <cfset format = workbook.createDataFormat() >
            <cfset cellintstyle = workbook.createCellStyle()>
            <cfset cellintstyle.setDataFormat(format.getFormat("0"))>
            <cfset cellfloatstyle = workbook.createCellStyle()>
            <cfset cellfloatstyle.setDataFormat(format.getFormat("0.0##"))>
            <cfset cellstyleData.setWrapText(true)/>

            <cfset cellstyleforallrows = workbook.createCellStyle()>
            <cfset fontfaceforallrows = workbook.createFont()>
            <cfset fontfaceforallrows.setColor(CreateObject( "java","org.apache.poi.hssf.util.HSSFColor$BLUE").getIndex())>
            <cfset cellstyleforallrows.setFont(fontfaceforallrows)>
            <cfset cellstyleforallrows.setWrapText(true)/>

                    <!--- creating new row object for the LEGEND title--->      
                    <cfset rowLegend = newSheetLegend.createRow(0)/>
                    <!--- Assigning the sheet name Legend--->
                    <cfset workBook.setSheetName(0, "LEGEND")/>
                    <!--- First Row First column text should be LEGEND. Giving that in bold --->
                    <cfset cellLegend = rowLegend.createCell(0)/> 
                    <cfset cellStyleStatic = createObject("java","org.apache.poi.xssf.usermodel.XSSFCellStyle")/>
                    <cfset cellLegend.setCellStyle(cellstyle)/>
                    <cfset cellLegend.setCellValue("LEGEND")/>
                    <cfset region = server.loader.create("org.apache.poi.ss.util.CellRangeAddress").init(0,0,0,48)/>
                    <cfset newSheetLegend.addMergedRegion(region)/>

网上也没有任何相关内容,有没有人遇到过这样的问题?如果有,请帮我解决这个问题。 感谢

这是sheet1的mergecell xml的xml内容:

<x:mergeCells xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
  <x:mergeCell ref="A1:AW1" />
  <x:mergeCell ref="B2:AW1" />
  <x:mergeCell ref="C3:AW1" />
  <x:mergeCell ref="D4:AW1" />
  <x:mergeCell ref="E5:AW1" />
  <x:mergeCell ref="F6:AW4" />
  <x:mergeCell ref="G7:C1" />
  <x:mergeCell ref="G7:AW4" />
  <x:mergeCell ref="H8:AW4" />
  <x:mergeCell ref="I9:AW4" />
  <x:mergeCell ref="J10:AW4" />
  <x:mergeCell ref="K11:AW4" />
  <x:mergeCell ref="L12:AW4" />
  <x:mergeCell ref="M13:AW4" />
  <x:mergeCell ref="N14:AW4" />
  <x:mergeCell ref="O15:AW4" />
  <x:mergeCell ref="P16:AW4" />
  <x:mergeCell ref="Q17:AW4" />
  <x:mergeCell ref="R18:AW4" />
  <x:mergeCell ref="S19:AW4" />
  <x:mergeCell ref="T20:AW4" />
  <x:mergeCell ref="U21:AW4" />
  <x:mergeCell ref="V22:AW4" />
  <x:mergeCell ref="W23:AW4" />
  <x:mergeCell ref="X24:AW4" />
  <x:mergeCell ref="Y25:AW4" />
  <x:mergeCell ref="Z26:AW4" />
  <x:mergeCell ref="AA27:AW4" />
  <x:mergeCell ref="AB28:AW4" />
  <x:mergeCell ref="AC29:AW4" />
  <x:mergeCell ref="AD30:AW4" />
  <x:mergeCell ref="AE31:AW4" />
  <x:mergeCell ref="AF32:AW4" />
  <x:mergeCell ref="AG33:AW4" />
  <x:mergeCell ref="AH34:AW4" />
  <x:mergeCell ref="AI35:AW4" />
  <x:mergeCell ref="AJ36:AW4" />
  <x:mergeCell ref="AK37:AW4" />
  <x:mergeCell ref="AL38:AW4" />
  <x:mergeCell ref="AM39:AW4" />
  <x:mergeCell ref="AN40:AW4" />
  <x:mergeCell ref="AO41:AW4" />
  <x:mergeCell ref="AP42:AW4" />
  <x:mergeCell ref="AQ43:AW4" />
  <x:mergeCell ref="AR44:AW4" />
  <x:mergeCell ref="AS45:AW4" />
  <x:mergeCell ref="AT46:AW4" />
  <x:mergeCell ref="AU47:AW4" />
  <x:mergeCell ref="AV48:AW4" />
  <x:mergeCell ref="AW49:AW4" />
  <x:mergeCell ref="AX50:AW4" />
  <x:mergeCell ref="AY51:AW4" />
  <x:mergeCell ref="AZ52:AW4" />
</x:mergeCells>

1 个答案:

答案 0 :(得分:6)

This thread表明较新的格式可能不太容忍重叠或重复。检查您的CF代码,看它是否无意中创建了任何重复或冲突的区域。

通过在电子表格中添加相同的区域两次,或者通过更新现有的文件并添加已存在的区域(或冲突),我能够在POI 3.9和3.13下重现错误with)电子表格中的现有区域。使用zip工具打开文件,并使用记事本检查/xl/worksheets/sheet1.xml文件,显示合并的单元节点已添加两次:

<mergeCells>
   <mergeCell ref="A1:AW1"/>
   <mergeCell ref="A1:AW1"/>
</mergeCells>

在“修复”文件后,删除了重复节点(当然错误消失了):

<mergeCells>
   <mergeCell ref="A1:AW1"/>
</mergeCells>

再次,检查添加区域的任何CF代码。如果您仍然不确定代码的哪一部分导致了问题,请尝试查看/xl/worksheets/sheet1.xml并搜索“mergeCells”。这应该指向正确的方向。

<强>更新

查看来自sheet1.xml的mergedcell节点,它看起来像许多区域重叠,或者可能无效。例如,前两个区域都在列AW,行1处结束。 (由于第二个区域在第2行开始,我不确定终点是否有效?)

<x:mergeCell ref="A1:AW1" />
<x:mergeCell ref="B2:AW1" />

无论如何,调整范围以使它们不重叠(例如,如果第AW列中的第二个范围结束,第2行)应解决错误。

<x:mergeCell ref="A1:AW1" />
<x:mergeCell ref="B2:AW2" />