Converting Excel File From .csv To .xlsx

后端 未结 6 1185
清歌不尽
清歌不尽 2020-12-05 18:54

I want my application to go and find a csv excel file and convert it into a .xlsx file instead.

Here\'s what I\'m currently doing;

var fileName = @\"         


        
6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-05 19:03

    For those who want to use Interop instead of an external library, you can simply do this:

    Application app = new Application();
    Workbook wb = app.Workbooks.Open(@"C:\testcsv.csv", 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);
    wb.SaveAs(@"C:\testcsv.xlsx", XlFileFormat.xlOpenXMLWorkbook, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
    wb.Close();
    app.Quit();
    

    The second argument of Workbook.SaveAs determines the true format of the file. You should make the filename extension match that format so Excel doesn't complain about corruption. You can see a list of the types and what they mean on MSDN.

    http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.xlfileformat.aspx

    As always, please keep Microsoft's considerations in mind if this functionality is intended for a server environment. Interop may not be the way to go in that situation:

    http://support.microsoft.com/kb/257757

提交回复
热议问题