views:

101

answers:

1

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!

A: 

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.

burnside
Exactly what I was looking for. Thank you!
James Alexander