日期:2014-05-20  浏览次数:20763 次

高分:如何将固定格式 excel 转化 xml
如题   最好有具体代码~

------解决方案--------------------
其实可以用POI 读出来,根据你自己的xml定义格式再写到xml文件中。
ps:可以用poi+dom4j或者jxl+dom4j
------解决方案--------------------
用jxl读EXCEL就行了,下载后里面有DEMO,研究下就可以了
至于转化为XML,要DOM4J,但是EXCEL转化成XML并非是社么都能转的,要符合
XML的规范
------解决方案--------------------
代码是C#的实现,JAVA调ActiveX,然后其他代码就类似了:
如下代码实现了Xls--> Xml或者Xml--> Xls的转化,它的运行需要引入Excel的ActiveX控件:

using System;
using System.Collections.Generic;
using System.Text;

/*Windows2003
using OfficeExcel = Microsoft.Office.Interop.Excel;
using ExcelApplication = Microsoft.Office.Interop.Excel.Application;
*/

/* WindowsXP系统用*/
using OfficeExcel = Excel;
using ExcelApplication = Excel.Application;

namespace Sysbug
{
public class Sysbug_XlsFormat
{
/*
文件格式转化:
Xml文件转化为Xls文件
*/
public static void XmlToXls(String XmlFileName, String XlsFielName, Boolean MsgVisable)
{

String XlsPathName = XlsFielName.Substring(0, XlsFielName.LastIndexOf(@ "\ "));
if (!Directory.Exists(DirectoryName))
{
Directory.CreateDirectory(DirectoryName);
}

ExcelApplication ExcelApp = new ExcelApplication();
ExcelApp.Visible = false;
try
{
object oMissiong = System.Reflection.Missing.Value;

/*Windows2003
ExcelApp.Workbooks.OpenXML(XmlFileName, 1, oMissiong);
*/

/*WindowsXP系统用*/
ExcelApp.Workbooks.OpenXML(XmlFileName, 1, oMissiong);


ExcelApp.DisplayAlerts = MsgVisable;
ExcelApp.Workbooks[1].SaveAs(
XlsFielName, OfficeExcel.XlFileFormat.xlWorkbookNormal, oMissiong, oMissiong,
oMissiong, oMissiong, OfficeExcel.XlSaveAsAccessMode.xlNoChange,
oMissiong, oMissiong, oMissiong, oMissiong, oMissiong);
ExcelApp.DisplayAlerts = true;
}
catch (Exception Exp)
{
if (Exp.Message.ToString().IndexOf( "0x800A03EC ") == -1)
{
throw new Exception( "You should close the " + XlsFielName + " file first ! ");
}
}
finally
{
ExcelApp.Workbooks.Close();
}
}

/*
文件格式转化:
Xls文件转化为Xml文件
*/
public static void XlsToXml(String XlsFileName, String XmlFielName)
{
ExcelApplication ExcelApp = new ExcelApplication();
ExcelApp.Visible = false;
try
{
object oMissiong = System.Reflection.Missing.Value;
ExcelApp.Workbooks.Open(XlsFileName,
oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong,
oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong);

ExcelApp.Workbooks[1].SaveAs(
XmlFielName, OfficeExcel.XlFileFormat.xlXMLSpreadsheet, null, null,
false, false, OfficeExcel.XlSaveAsAccessMode.xlExclusive,