简体   繁体   中英

Error in converting Excel data to CSV using C# code

I am using this question to convert Excel data to CSV.

   static void ConvertExcelToCsv(string excelFilePath, string csvOutputFile, int worksheetNumber = 1) 
    {
   var cnnStr = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;IMEX=1;HDR=NO\"", excelFilePath);
   var cnn = new OleDbConnection(cnnStr);
   var dt = new DataTable();
  try {
  cnn.Open();
       }   
     }

I am getting the following exception once the cnn.open() line gets executed:

"External table is not in expected format"

The question in the link is old and using Excel 8.0 Spreadsheet version as Octopoid stated. You can try with the version 12.0 or figure out which version you use :

var cnnStr = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};" + "Extended Properties='Excel 12.0 Xml;HDR=YES;'");



If you can't manage to make it work, you can always use a library :>

Library is called "excellibrary"
Here is the link : https://code.google.com/p/excellibrary/

It is pretty simple to use it. It has a Wiki in the given link so you can get familiar with the usage.

Oğuzhan

嗨,您可以打开.xls文件,然后使用C#将其另存为.csv

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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