代码快速实现xml转换为Excelxml转excel通用类java完成代码可作工具使用.docx
- 文档编号:6285215
- 上传时间:2023-01-05
- 格式:DOCX
- 页数:24
- 大小:87.60KB
代码快速实现xml转换为Excelxml转excel通用类java完成代码可作工具使用.docx
《代码快速实现xml转换为Excelxml转excel通用类java完成代码可作工具使用.docx》由会员分享,可在线阅读,更多相关《代码快速实现xml转换为Excelxml转excel通用类java完成代码可作工具使用.docx(24页珍藏版)》请在冰豆网上搜索。
代码快速实现xml转换为Excelxml转excel通用类java完成代码可作工具使用
代码快速实现xml转换为Excel(xml转excel通用类-java-完成代码可作工具使用)
用代码实现xml文件/数据转换为excel文件。
(java)----何潮
背景:
最近项目要做导出功能,但导出的数据对象类型实在太多了,一个个去实现;实在是没心情去做。
于是----------
意义:
快速实现数据导出
为什么是xmltoexcel?
因为项目中可以直接使用xml数据。
所以就选择xml了。
(直接数据库取数据转excel,也可以按同一思路实现)
目标:
从一个最多二级关联的数据结构,通过定义结构关系,实现数据自动转换成excel.
特点:
支持字典字段转换,可合并多关联字段,三种效果展示,有一定的可复用性^_^(最多二级关联----当然,可自行扩展)
转换效果:
实现三种显示方式的转换
方式1:
一行显示一个完整对象(合并多个关联表)
方式2:
复合形展示
方式3:
一行显示一个完整对象(合并多个关联表)----支持不同对象不同表头
复用实现一个简单导出要多少代码,
xmlStructmainXmlStruct1=newxmlStruct();
mainXmlStruct1.tableName="CM10_OBJECT";
mainXmlStruct1.tableCName="活动信息";
mainXmlStruct1.tableCalssField="OBJ_SUBTYPE";
mainXmlStruct1.tableCalssCName.put("200101","院团大事");
HashMap
test1.put("0","新建");
test1.put("100","完成");
mainXmlStruct1.tableFilesClassValue.put("OBJ_STATE",test1);
mainXmlStruct1.tableFiles=newString[][]{{"OBJ_NAME","活动名称"},{"FIELD_1","活动内容简介"},{"FIELD_2","活动开始时间"},
{"FIELD_3","活动结束时间"},{"OBJ_STATE","状态"}};
xt.alltables.put("200101",mainXmlStruct1);
思路,
定义结构定义数据关系
过程循环取表,循环取表头,循环取内容--------
classxExcelType
{
/**xls显示方式0:
组合式1:
单一列表式**/
intexShowType=0;
StringrootName="DataSource";
StringmaintableName="CM10_OBJECT";
HashMap
}
/**定义xml数据结构定义**/
classxmlStruct
{
StringtableName="";//表名
StringtableCName="";//表名中文说明
/**表的性质0:
主表(默认)1:
从表---这里暂处理1级主从,哈**/
inttableType=0;
StringtableCalssField="";//如果有内容分类,分类标识字段
StringtableCalssFieldValue="";
HashMap
/**表的字典字段及对应值**/
HashMap
/**表字段中文对应**/
//HashMap String>(); String[][]tableFiles; } 理论上通用,罪过。 不多说,有可能用到的,下附件看看。 不合理的地方请指出---------------- 下面是完整代码(一些特殊引用只是用作数据取数据,引用时可以去掉的----)packagejetsennet.jmcc.business; importjava.io.File; importjava.io.IOException; importjava.sql.SQLException; importjava.util.HashMap; importjava.util.Iterator; importjava.util.List; importjava.util.Map; importjavax.jws.WebParam; importmon.PathUtil;importjetsennet.contentmanage.CmDataAccess;importjetsennet.contentmanage.CmObjHelper;import.UserAuthHeader;import.WSResult; importjetsennet.sqlclient.ConnectionInfo;importjetsennet.sqlclient.DbConfig;importjetsennet.sqlclient.ISqlExecutor;importjetsennet.sqlclient.QueryTable;importjetsennet.sqlclient.SqlClientObjFactory;importjetsennet.sqlclient.SqlCondition;importjetsennet.sqlclient.SqlLogicType;importjetsennet.sqlclient.SqlParamType;importjetsennet.sqlclient.SqlQuery;importjetsennet.sqlclient.SqlRelationType;importjetsennet.sqlclient.TableJoinType;importjetsennet.util.StringUtil;importjxl.Workbook; importjxl.write.Label; importjxl.write.WritableCellFormat;importjxl.write.WritableFont; importjxl.write.WritableSheet; importjxl.write.WritableWorkbook; importorg.dom4j.Document; importorg.dom4j.Element; importorg.dom4j.io.SAXReader; publicclassxmlToExcel { privatestaticjetsennet.logger.ILoglogger=jetsennet.logger.LogManager.getLogger("JetsenNet.JCMP"); privatestaticStringXSD_FILE_PATH="jcmp/schemafiles"; privatestaticConnectionInfocmpConnectionString=newConnectionInfo(DbConfig.getProperty("cmp_driver"),DbConfig.getProperty("cmp_dburl"), DbConfig.getProperty("cmp_dbuser"), DbConfig.getProperty("cmp_dbpwd")); staticjxl.write.WritableFonttitleFont=newjxl.write.WritableFont(WritableFont.createFont("宋体"),10,WritableFont.BOLD,false); staticWritableCellFormattitleFormat=newjxl.write.WritableCellFormat(titleFont); privatestaticConnectionInfomccConnectionString=newConnectionInfo(DbConfig.getProperty("mcc_driver"),DbConfig.getProperty("mcc_dburl"), DbConfig.getProperty("mcc_dbuser"), DbConfig.getProperty("mcc_dbpwd")); privatestaticISqlExecutorsqlExecutor=SqlClientObjFactory.createSqlExecutor(mccConnectionString); staticintcelltitlenow=0;//当前列表头列号 staticintcellvaluenow=0;//当前内容列号 staticintrowtitlenow=0;//当前列表头行号 staticbooleanaddcelltitle=true; staticStringOldObjtype=""; publicstaticvoidmain(String[]args) { Filefile=newFile("C: /temp/test.xls"); WritableWorkbookworkbook; try { workbook=Workbook.createWorkbook(file); //processActivity(workbook,"4023,4025,4007,4024,4026","200101,200103,200101,200102,200104"); processActivity(workbook,"4023,4025,4007","200101,200101,200101"); workbook.write(); workbook.close(); } catch(Exceptione) { //TODOAuto-generatedcatchblock e.printStackTrace(); } System.out.println("OK? "); } publicstaticvoidprocessActivity(WritableWorkbookworkBook,String obj_id,Stringobj_subtype)throwsException { OldObjtype=""; celltitlenow=0; addcelltitle=true; rowtitlenow=0; WritableSheetsheet=workBook.createSheet("sheet1",0); /****/ /**导出ID**/ String[]ids=obj_id.split(","); /**导出类型**/ String[]idt=obj_subtype.split(","); /**导出个数**/ intidsl=ids.length; /**定义列宽**/ intcellwihth=20; xExcelTypext=newxExcelType(); xt.exShowType=1; if(xt.exShowType==1) { addcelltitle=true; } else { addcelltitle=false; } //******************主表/结构定义 //200101 xmlStructmainXmlStruct1=newxmlStruct(); mainXmlStruct1.tableName="CM10_OBJECT"; mainXmlStruct1.tableCName="活动信息"; mainXmlStruct1.tableCalssField="OBJ_SUBTYPE"; mainXmlStruct1.tableCalssCName.put("200101","院团大事"); mainXmlStruct1.tableCalssCName.put("200102","院团出访"); mainXmlStruct1.tableCalssCName.put("200103","院团专家交流"); HashMap test1.put("0","新建"); test1.put("100","完成"); mainXmlStruct1.tableFilesClassValue.put("OBJ_STATE",test1); mainXmlStruct1.tableFiles=newString[][]{{"OBJ_NAME","活动名称"},{"FIELD_1","活动内容简介"},{"FIELD_2","活动开始时间"}, {"FIELD_3","活动结束时间"}}; xt.alltables.put("200101",mainXmlStruct1); xt.alltables.put("200102",mainXmlStruct1); xt.alltables.put("200103",mainXmlStruct1); //200104 xmlStructmainXmlStruct2=newxmlStruct(); mainXmlStruct2.tableName="CM10_OBJECT"; mainXmlStruct2.tableCName="活动信息"; mainXmlStruct2.tableCalssField="OBJ_SUBTYPE"; mainXmlStruct2.tableCalssCName.put("200104","院团演出"); mainXmlStruct2.tableFiles=newString[][]{{"OBJ_NAME","活动名称"},{"FIELD_2","活动开始时间"},{"FIELD_3","活动结束时间"}}; xt.alltables.put("200104",mainXmlStruct2); //20010401 xmlStructmainXmlStruct3=newxmlStruct(); mainXmlStruct3.tableName="CM10_OBJECT"; mainXmlStruct3.tableCalssField="OBJ_SUBTYPE"; mainXmlStruct3.tableCalssCName.put("20010401","演出日志"); //HashMap //mainXmlStruct1.tableFilesClassValue.put("FIELD_3",showType_hm); mainXmlStruct3.tableFiles=newString[][]{{"FIELD_1","演出时间"},{"FIELD_2","演出场次"},{"FIELD_3","演出类型"},{"NUM_VAL2","观众人数"}}; xt.alltables.put("20010401",mainXmlStruct3); //********************从表/结构定义 // xmlStructson1Struct1=newxmlStruct(); son1Struct1.tableName="CM10_MULTIINFO"; son1Struct1.tableCName="动信息"; son1Struct1.tableCalssField="INFO_TYPE"; son1Struct1.tableCalssFieldValue="1"; //HashMap //test2.put("1","新建1"); //test2.put("2","完成2"); //test2.put("3","完成3"); //xs2.tableFilesClassValue.put("INFO_TYPE",test2); son1Struct1.tableFiles=newString[][]{{"FIELD_1","活动地点"}}; xt.alltables.put("200101-1",son1Struct1); xt.alltables.put("200102-1",son1Struct1); xt.alltables.put("200103-1",son1Struct1); // xmlStructson1Struct2=newxmlStruct(); son1Struct2.tableName="CM10_MULTIINFO"; son1Struct2.tableCName="人员信息"; son1Struct2.tableCalssField="INFO_TYPE"; son1Struct2.tableCalssFieldValue="2"; son1Struct2.tableFiles=newString[][]{{"FIELD_1","参与人员"}}; xt.alltables.put("200101-2",son1Struct2); xt.alltables.put("200102-2",son1Struct2); xt.alltables.put("200103-2",son1Struct2); // xmlStructson1Struct3=newxmlStruct(); son1Struct3.tableName="CM10_MULTIINFO"; son1Struct3.tableCName="剧目信息"; son1Struct3.tableCalssField="INFO_TYPE"; son1Struct3.tableCalssFieldValue="3"; son1Struct3.tableFiles=newString[][]{{"FIELD_1","剧目名称"}}; xt.alltables.put("200101-3",son1Struct3); xt.alltables.put("200102-3",son1Struct3); xt.alltables.put("200103-3",son1Struct3); xmlStructson2Struct1=newxmlStruct(); son2Struct1.tableName="CM10_MULTIINFO"; son2Struct1.tableCalssField="INFO_TYPE"; son2Struct1.tableCalssFieldValue="1"; son2Struct1.tableFiles=newString[][]{{"FIELD_1","节目单"}}; xt.alltables.put("200104-1",son2Struct1); xmlStructson2Struct2=newxmlStruct(); son2Struct2.tableName="CM10_MULTIINFO"; son2Struct2.tableCalssField="INFO_TYPE"; son2Struct2.tableCalssFieldValue="2"; son2Struct2.tableFiles=newString[][]{{"FIELD_1","剪报"}}; xt.alltables.put("200104-2",son2Struct2); xmlStructson2Struct3=newxmlStruct(); son2Struct3.tableName="CM10_MULTIINFO"; son2Struct3.tableCalssField="INFO_TYPE"; son2Struct3.tableCalssFieldValue="3"; son2Struct3.tableFiles=newString[][]{{"FIELD_1","海报"}}; xt.alltables.put("200104-3",son2Struct3); //演出的日志多关联 xmlStructson3Struct1=newxmlStruct(); son3Struct1.tableName="CM10_MULTIINFO"; son3Struct1.tableCalssField="INFO_TYPE"; son3Struct1.tableCalssFieldValue="1"; son3Struct1.tableFiles=newString[][]{{"FIELD_
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 代码 快速 实现 xml 转换 Excelxml excel 通用 java 完成 工具 使用
![提示](https://static.bdocx.com/images/bang_tan.gif)