在C#中使用Excel文件

我有2000和2003格式的Excel文件。 我需要通过C#代码将它们导入访问数据库。 我已经写了一个方法来读取文件到数据表中。 无论我使用哪个连接string(我已经检查了关于这个主题的其他post),我仍然得到“表格格式不正确”的错误。 有人可以向我解释我做错了什么。

public static DataSet ParseExcel(string excelFile) { string sheetName = Path.GetFileNameWithoutExtension(excelFile); string excelQuery = @"SELECT * FROM [" + sheetName + "]"; string excelConnctionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "" + excelFile + "" + @";Extended Properties=" + "" + @"Excel 8.0;HDR=Yes;" + ""; if(File.Exists(excelFile)) { var myConnection = new OleDbConnection(excelConnctionString); myConnection.Open(); var myCommand = new OleDbDataAdapter(excelQuery, excelConnctionString); myCommand.TableMappings.Add("Table", "TestTable"); var dtSet = new DataSet(); myCommand.Fill(dtSet); myConnection.Close(); return dtSet; } return null; } 

仔细阅读此代码示例,并尝试了解工作stream程。 根据您的要求,您可以很容易地编写任何types的访问Excel数据的程序。

1.我有一个上传字段,以select.aspx文件中的Excel文件

 <asp:FileUpload ID="Upload_File" runat="server" /> <asp:Button ID="Upload_Button" runat="server" Text="Upload" onclick="btnUpload_Click"/> <asp:GridView ID="Gridview_Name" runat="server"> </asp:GridView> 
  1. 现在让我们看看代码在文件后面(.aspx.cs文件)

protected void Upload_Button_Click(object sender,EventArgs e){string connectionString =“”; if(Upload_File.HasFile)//检查是否select要上传的文件

 { //getting name of the file string fileName = Path.GetFileName(Upload_File.PostedFile.FileName); //getting extension of the file (for checking purpose - which type .xls or .xlsx) string fileExtension = Path.GetExtension(Upload_File.PostedFile.FileName); string fileLocation = Server.MapPath("" + fileName); //exact location of the excel files Upload_File.SaveAs(fileLocation); //Check whether file extension is xls or xslx if (fileExtension == ".xls") { connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\""; } else if (fileExtension == ".xlsx") { connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\""; } //Create OleDB Connection and OleDb Command OleDbConnection con = new OleDbConnection(connectionString); OleDbCommand cmd = new OleDbCommand(); //cmd.CommandType = System.Data.CommandType.Text; cmd.Connection = con; OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd); DataTable dtExcelRecords = new DataTable(); con.Open(); DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); string getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString(); cmd.CommandText = "SELECT * FROM [" + getExcelSheetName + "]"; dAdapter.SelectCommand = cmd; dAdapter.Fill(dtExcelRecords); con.Close(); Gridview_Name.DataSource = dtExcelRecords; GridView_Name.DataBind(); } else { Response.Write("Please Select a File to extract data "); } } 

逐步说明:

  • 我们得到文件名,扩展名,位置。
  • 并检查它是types(.xls或.xlsx – 特别是对于2003或其他格式的Excel)。
  • 根据上一步设置连接string。
  • 打开oledb连接
  • 创build必要的数据适配器和数据表
  • 打开连接
  • 将当前表(Excel中的数据存储在该表中)存储在数据表示实例中
  • 通过从当前表中获取工作表,以astring存储工作表的名称
  • 用数据表(dtExcelRecords)填充数据适配器对象(dAdapter)
  • closures连接
  • 将数据源的数据源设置为数据表。
  • 绑定它与我们的网格。

…我们完成了!

希望能帮助到你。

尝试这个

 OLEDB;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;Extended Properties="Excel 8.0;HDR=Yes;IMEX=1";