我正在使用Java POI库创建一个Excel文件,虽然文件已经创建成功,但是当我打开文件时,Excel弹出一条消息:
"我们在文件的内容中发现了一些问题。如果您信任工作簿的来源,是否希望我们尝试尽可能多地进行修复?"
点击“是”后,弹出以下信息:"Repaired Part: /xl/worksheets/sheet1.xml part with XML error. Load error. Line 121, column 0. Removed Records: Merge cells from /xl/worksheets/sheet1.xml part"。
虽然文件中没有数据丢失,但我无法理解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合并单元格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>