2011-07-16 74 views
3

在我的應用程序中,我可以導入csv文件,但我在Excel表中有數據,所以我需要將其轉換爲csv格式。 我得到了來自淨代碼爲Excel導出數據到CSV時,我下載的是壓縮文件,然後運行它的工作,但是當我這個程序複製到VS 2008和運行它,它不工作將Excel工作表數據導出爲csv格式

的代碼是

using System; 
using System.IO; 
using System.Data; 
using System.Data.OleDb; 
using System.Collections.Generic; 
using System.Text; 

namespace XlsToCsv 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string sourceFile, worksheetName, targetFile; 
      sourceFile = @"D:\emp.xls";worksheetName = "sheet1"; targetFile = @"D:\empcsv.csv"; 
      convertExcelToCSV(sourceFile, worksheetName, targetFile); 
     } 

     static void convertExcelToCSV(string sourceFile, string worksheetName, string targetFile) 
     { 
      string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sourceFile + ";Extended Properties=\" Excel.0;HDR=Yes;IMEX=1\""; 
      OleDbConnection conn = null; 
      StreamWriter wrtr = null; 
      OleDbCommand cmd = null; 
      OleDbDataAdapter da = null; 
      try 
      { 
       conn = new OleDbConnection(strConn); 
       conn.Open(); 

       cmd = new OleDbCommand("SELECT * FROM [" + worksheetName + "$]", conn); 
       cmd.CommandType = CommandType.Text; 
       wrtr = new StreamWriter(targetFile); 

       da = new OleDbDataAdapter(cmd); 
       DataTable dt = new DataTable(); 
       da.Fill(dt); 

       for (int x = 0; x < dt.Rows.Count; x++) 
       { 
        string rowString = ""; 
        for (int y = 0; y < dt.Columns.Count; y++) 
        { 
         rowString += "\"" + dt.Rows[x][y].ToString() + "\","; 
        } 
        wrtr.WriteLine(rowString); 
       } 
       Console.WriteLine(); 
       Console.WriteLine("Done! Your " + sourceFile + " has been converted into " + targetFile + "."); 
       Console.WriteLine(); 
      } 
      catch (Exception exc) 
      { 
       Console.WriteLine(exc.ToString()); 
       Console.ReadLine(); 
      } 
      finally 
      { 
       if (conn.State == ConnectionState.Open) 
        conn.Close(); 
       conn.Dispose(); 
       cmd.Dispose(); 
       da.Dispose(); 
       wrtr.Close(); 
       wrtr.Dispose(); 
      } 
     } 
    } 
} 
這樣

System.Data.OleDb.OleDbException: Could not find installable ISAM. 

    at System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString cons 
tr, OleDbConnection connection) 

    at System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOpti 
ons options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection o 
wningObject) 

    at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbC 
onnection owningConnection, DbConnectionPoolGroup poolGroup) 

    at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection ow 
ningConnection) 

    at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection ou 
terConnection, DbConnectionFactory connectionFactory) 

    at System.Data.OleDb.OleDbConnection.Open() 

    at Excel_To_csv.Program.convertExcelToCSV(String sourceFile, String worksheet 
Name, String targetFile) in D:\Excel to csv\Excel To csv\Excel To csv\Program.cs 
:line 41 

其投擲的錯誤這是爲什麼錯誤來了,我不知道

回答

1

您的錯誤可能是由於excel驅動程序問題引起的,但並非100%確定,但是由於某些所需的dll缺少您的計算機而導致此錯誤。

您可以通過將MDAC安裝到您的計算機並嘗試執行此操作來解決此問題。

如果它沒有解決,你可以使用下面的粘貼代碼,這是我寫的只是作爲你的問題的答案,但首先我應該告訴你下面的代碼部分是不高效的,如果轉換文件有相當多的數字記錄的前65000

要使用下面的代碼部分,你需要添加下面參考

using Excel = Microsoft.Office.Interop.Excel; 
using Office = Microsoft.Office.Core; 

功能

private void EXCELTOCSV() 
    { 
     OpenFileDialog excelSheetToOpen = new OpenFileDialog(); 
     excelSheetToOpen.Filter = "Excel 97- 2003 WorkBook (*.xls)| *.xls | Excel 2007 WorkBook (*.xlsx) | *.xlsx | All files (*.*)|*.*"; 
     excelSheetToOpen.FilterIndex = 3; 
     excelSheetToOpen.Multiselect = false; 



     if (excelSheetToOpen.ShowDialog() == DialogResult.OK) 
     { 

      Excel.Application excelApp = new Excel.Application(); 
      String workbookPath = excelSheetToOpen.FileName; 
      Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath); 
      Excel.Sheets excelWorkBookSheets = excelWorkbook.Sheets; 

      Excel.Range _UsedRangeOftheWorkSheet; 


      foreach (Excel.Worksheet _Sheet in excelWorkBookSheets) 
      { 
       if (_Sheet.Name =="ExcelSheetName") 
       { 

        _UsedRangeOftheWorkSheet = _Sheet.UsedRange; 

        Object[,] s = _UsedRangeOftheWorkSheet.Value; 

        System.IO.StreamWriter sw = new System.IO.StreamWriter("FileName.csv", true); 

        for (int b = 0; b < s.Length; b++) 
        { 
         StringBuilder sb = new StringBuilder(); 
         for (int c = 0; c < s.Rank; c++) 
         { 
          if (sb == null) 
          { 
           sb.Append((String)s[b, c]); 
          } 
          else 
          { 
           sb.Append("," + (String)s[b, c]); 
          } 
         } 
         sw.WriteLine(sb.ToString()); 
        } 
        sw.Close(); 

       } 
      } 

     } 
    } 

何PE這將爲你工作

謝謝。

-1

看起來像你問題是在連接字符串中的擴展屬性。

你已經寫Excel.0它不應該是Excel X.0其中X是版本8.0 number.Like

+0

@謝卡爾 - 我改變了,但得到相同的錯誤.. – Sweety

0

這裏是另一個計算器線程進入這些類型的錯誤一定的深度。 Writing into excel file with OLEDB

使用這些較舊的連接庫的代碼往往會遇到這些問題。

相關問題