Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a C# method to do this. Remember to add your own error handling - this mostly assumes that things work for the sake of brevity. It's 4.0+ framework only, but that's mostly because of the optional <code>worksheetNumber</code> parameter. You can overload the method if you need to support earlier versions.</p> <pre><code>static void ConvertExcelToCsv(string excelFilePath, string csvOutputFile, int worksheetNumber = 1) { if (!File.Exists(excelFilePath)) throw new FileNotFoundException(excelFilePath); if (File.Exists(csvOutputFile)) throw new ArgumentException("File exists: " + csvOutputFile); // connection string 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); // get schema, then data var dt = new DataTable(); try { cnn.Open(); var schemaTable = cnn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); if (schemaTable.Rows.Count &lt; worksheetNumber) throw new ArgumentException("The worksheet number provided cannot be found in the spreadsheet"); string worksheet = schemaTable.Rows[worksheetNumber - 1]["table_name"].ToString().Replace("'", ""); string sql = String.Format("select * from [{0}]", worksheet); var da = new OleDbDataAdapter(sql, cnn); da.Fill(dt); } catch (Exception e) { // ??? throw e; } finally { // free resources cnn.Close(); } // write out CSV data using (var wtr = new StreamWriter(csvOutputFile)) { foreach (DataRow row in dt.Rows) { bool firstLine = true; foreach (DataColumn col in dt.Columns) { if (!firstLine) { wtr.Write(","); } else { firstLine = false; } var data = row[col.ColumnName].ToString().Replace("\"", "\"\""); wtr.Write(String.Format("\"{0}\"", data)); } wtr.WriteLine(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload