开发者

How do I use PIA to open an Excel workbook and save the first sheet to a text file?

开发者 https://www.devze.com 2022-12-18 10:39 出处:网络
I\'m not too familiar w/ the PIA assembly\'s for Office interop (Office 2007) and via .NET/C#, I need to open a开发者_如何学编程 workbook and save the first sheet within it as a tab delimited text fil

I'm not too familiar w/ the PIA assembly's for Office interop (Office 2007) and via .NET/C#, I need to open a开发者_如何学编程 workbook and save the first sheet within it as a tab delimited text file. Can someone instruct me on how to do this? Thanks!


That's quite a big ask mate, but have a look at the below.

[TestMethod()]
    public void ExcelTest()
    {
        Microsoft.Office.Interop.Excel.Application excelApplication = new Application();

        string file = @"C:\testsheet.xls";

        Microsoft.Office.Interop.Excel.Workbook wkb = excelApplication.Workbooks.Open(
                file, 0, false, 5, Missing.Value, Missing.Value, false,
                Missing.Value, Missing.Value, true, false, Missing.Value, false, false, false);

        Microsoft.Office.Interop.Excel.Worksheet wks = wkb.Worksheets[1] as Microsoft.Office.Interop.Excel.Worksheet;

        wks.SaveAs(@"C:\savedFile.txt", Microsoft.Office.Interop.Excel.XlFileFormat.xlTextWindows
            , Type.Missing, Type.Missing, 
            Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
    }

Remember to close the application and workbook when you're done or on any errors.

0

精彩评论

暂无评论...
验证码 换一张
取 消