C#从我自己的XML文件创建一个excel文件

时间:2015-07-06 07:05:21

标签: c# xml excel

我编写了一个程序,该程序从数据库中查询并创建一个包含查询结果的xml文件。如何创建一个包含xml文件中元素的宏启用excel文件?

以下是xml文件的简短示例,该文件位于我的驱动器C :(我用XXXX替换了一些部分)

<JobSvc environment="TEST" dateTo="6/22/2015" dateFrom="2015-06-15T00:00:00+08:00">
  <process server="XXXX" type="XXXX" name="XXXX">
    <processId duration="2421" eventTime="6/1/2015 11:41:26 AM" source="TEST SOURCE" num="3">PROCESS ID</processId>
  </process>
</JobSvc>

这是我尝试从xml文件生成excel文件(我从互联网上获取它,不幸的是,它不起作用,但它也不会产生异常或错误,因为我的程序为异常创建日志..) :

public void xmlToExcel()
        {
            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;
            DataSet ds = new DataSet();
            XmlReader xmlFile;
            int i = 0;
            int j = 0;
            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            xmlFile = XmlReader.Create(@"C:\PerformanceMonitorXML " + DateTime.Now.ToString("dd_MM_yyyy") + ".xml", new XmlReaderSettings());
            ds.ReadXml(xmlFile);
            for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
            {
                for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
                {
                    xlWorkSheet.Cells[i + 1, j + 1] = ds.Tables[0].Rows[i].ItemArray[j].ToString();
                }
            }
            xlWorkBook.SaveAs("C\\PerformanceMonitorExcel " + DateTime.Now.ToString("dd_MM_yyyy") + ".xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();
            releaseObject(xlApp);
            releaseObject(xlWorkBook);
            releaseObject(xlWorkSheet);



        }

        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
            }
            finally
            {
                GC.Collect();
            }
        }

1 个答案:

答案 0 :(得分:0)

问题很可能出在你的特权上。如果要保存在C上,则必须使用管理员权限运行VS.另外还有一个拼写错误。另外我建议不要逐个单元格地写入excel,而是在对象数组中。下面的代码对我有用&gt;

Microsoft.Office.Interop.Excel.Application xlApp;
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
DataSet ds = new DataSet();
XmlReader xmlFile;
xlApp = new Microsoft.Office.Interop.Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xmlFile = XmlReader.Create(@"C:\example.xml", new XmlReaderSettings());
ds.ReadXml(xmlFile);
object[,] objects = new object[ds.Tables[0].Rows.Count, ds.Tables[0].Columns.Count];
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
    for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
    {
        objects[i, j] = ds.Tables[0].Rows[i][j].ToString();
    }
}
Microsoft.Office.Interop.Excel.Range range = xlWorkSheet.get_Range("A1", "D3");
range.Value = objects;
releaseObject(range);
xlWorkBook.SaveAs("C:\\PerformanceMonitorExcel " + DateTime.Now.ToString("dd_MM_yyyy") + ".xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlApp);
releaseObject(xlWorkBook);
releaseObject(xlWorkSheet);
MessageBox.Show("finished");

示例xml是&gt;

<root>
  <JobSvc environment="TEST" dateTo="6/22/2015" dateFrom="2015-06-15T00:00:00+08:00">
    <process server="XXXX" type="XXXX" name="XXXX">
      <processId duration="2421" eventTime="6/1/2015 11:41:26 AM" source="TEST SOURCE" num="3">PROCESS ID</processId>
    </process>
  </JobSvc>
  <JobSvc environment="TEST" dateTo="6/23/2015" dateFrom="2015-06-15T00:00:00+08:00">
    <process server="XXXX" type="XXXX" name="XXXX">
      <processId duration="2421" eventTime="6/1/2015 11:41:26 AM" source="TEST SOURCE" num="3">PROCESS ID</processId>
    </process>
  </JobSvc>
  <JobSvc environment="TEST" dateTo="6/24/2015" dateFrom="2015-06-15T00:00:00+08:00">
    <process server="XXXX" type="XXXX" name="XXXX">
      <processId duration="2421" eventTime="6/1/2015 11:41:26 AM" source="TEST SOURCE" num="3">PROCESS ID</processId>
    </process>
  </JobSvc>
</root>

Br,Márton