简体   繁体   English

将Excel中的特定工作表转换为CSV

[英]Convert particular sheet in excel to csv

How do I convert a particular sheet to CSV from Excel using C# Interop? 如何使用C#Interop将特定的工作表从Excel转换为CSV? Does it default to the first sheet or can I specify? 它默认为第一张纸还是我可以指定?

This is the code i am using: 这是我正在使用的代码:

 private void ConvertProgsToCSV()
    {
        Excel.Application app = new Excel.Application();
        Excel.Workbook wb = app.Workbooks.Open(txtFname.Text);
        wb.SaveAs(@"C:\Temp\output.csv", Excel.XlFileFormat.xlCSVWindows);
        wb.Close(false);
        app.Quit();
        Console.WriteLine("Done!");
    }

You have to open worksheet by it's name and Select it using Select() function: 您必须按其名称打开工作表,然后使用Select()函数选择它:

xlSheet = (Excel.Worksheet)xlBook.Worksheets["Sheet1"];
xlSheet.Select(Type.Missing);
xlBook.SaveAs("C:\Filename.csv", Excel.XlFileFormat.xlCSV,Excel.XlSaveAsAccessMode.xlNoChange);

Assuming that xlSheet is of type Interop.Excel.WorkSheet and xlBook is of type Interop.Excel.Workbook 假设xlSheet的类型为Interop.Excel.WorkSheetxlBook的类型为Interop.Excel.Workbook

You can refer to this links for more info: 您可以参考此链接以获取更多信息:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM