c# - 使用C#从excel表中读取数据

标签 c# excel

如何使用C#代码读取excel表中的信息......

最佳答案

您可以使用 Oledb

    using System.Data;
    using System.Data.OleDb;

OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Book1.xls;Extended Properties=Excel 8.0");
OleDbDataAdapter da = new OleDbDataAdapter("select * from MyObject", con);
DataTable dt = new DataTable();
da.Fill(dt);

或者您使用 Office Interop

this.openFileDialog1.FileName = "*.xls";
  if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
   {
      Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(
         openFileDialog1.FileName, 0, true, 5,
          "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false,
          0, true); 
     Excel.Sheets sheets = theWorkbook.Worksheets;
     Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1);
     for (int i = 1; i <= 10; i++)
     {
     Excel.Range range = worksheet.get_Range("A"+i.ToString(), "J" + i.ToString());
     System.Array myvalues = (System.Array)range.Cells.Value;
     string[] strArray = ConvertToStringArray(myvalues);
     }
}

关于c# - 使用C#从excel表中读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3666833/

相关文章:

excel - IIS 无法为用户获取对 Excel 文件的访问权限,但对我来说没问题。为什么?

c# - 覆盖 FindByEmail 以按租户查找

excel - 使用 VBA 从 Excel 表中导入 Sharepoint 2010 列表数据

c# - 如何在 C# 中使用 LdapConnection 对象在 LDAP 中搜索用户?

c# - 重定向另一个页面时清除母版页中的文本框

如果管理员用户注销,服务器上的 Excel 应用程序将无法工作

java - 如何读取应用服务器上存储的Excel表格

java - 使用 POI 检测和设置列宽并根据该宽度应用文本换行

c# - WPF 将 bool 值传递给 View 并更改它

c# - Fluent NHibernate OptimisticLock.None() 导致 "The string ' none' 不是有效的 bool 值。”