2013年3月12日星期二

6. C# Excel Save As XML


In my last post, I show a way to convert Excel as XML by OLDEDB and DataSet.
Here is another way to save Excel as XML, no OLEDB, no DataSet necessary.
Remember to reference
C:\Program Files\Microsoft Visual Studio 9.0\Visual Studio Tools for Office\PIA\Office12\Microsoft.Office.Interop.Excel.dll

Or

C:\Program Files\Microsoft Visual Studio 9.0\Visual Studio Tools for Office\PIA\Office11\Microsoft.Office.Interop.Excel.dll

I try it in both *.xsl and *.xslx format, all works fine.

public void MySaveExcelAsXml()
        {
            Microsoft.Office.Interop.Excel.Application _excelApp = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook workBook = _excelApp.Workbooks.Open(@"G:\input.xls",
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing);

            workBook.SaveAs(@"G:\test.xml",
                Microsoft.Office.Interop.Excel.XlFileFormat.xlXMLSpreadsheet,
                null,
                null,
                true,
                true,
                Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
                Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlLocalSessionChanges,
                true,
                null,
                null,
                false);
            workBook.Close(null, null, null);  

        }

没有评论:

发表评论