c# – 如何导入HTML格式的Excel

前端之家收集整理的这篇文章主要介绍了c# – 如何导入HTML格式的Excel前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经使用HttpContext从数据库导出数据,格式为table,tr和td.我想读同一个文件并转换成datatable.
  1. <add name="Excel03ConString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='HTML Import;HDR={1};IMEX=1'" />
  2.  
  3. <add name="Excel03ConString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR={1};IMEX=1'" />
  4.  
  5. private DataTable GetTableFromExcel()
  6. {
  7. DataTable dt = new DataTable();
  8.  
  9. try
  10. {
  11. if (exclFileUpload.HasFile)
  12. {
  13. string FileName = Path.GetFileName(exclFileUpload.PostedFile.FileName);
  14. string Extension = Path.GetExtension(exclFileUpload.PostedFile.FileName);
  15. string FolderPath = Server.MapPath(ConfigurationManager.AppSettings["FolderPath"]);
  16. //string NewFileName = string.Format("{0}_{1}",DateTime.Now.ToString().Replace("/","").Replace(" ","").Replace(":",""),FileName);
  17. string FilePath = Path.Combine(string.Format("{0}/{1}",FolderPath,FileName));
  18. exclFileUpload.SaveAs(FilePath);
  19. string conStr = "";
  20. switch (Extension)
  21. {
  22. case ".xls": //Excel 97-03
  23. conStr = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
  24. break;
  25. case ".xlsx": //Excel 07
  26. conStr = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
  27. break;
  28. }
  29. conStr = String.Format(conStr,FilePath,true);
  30. OleDbConnection connExcel = new OleDbConnection(conStr);
  31. OleDbCommand cmdExcel = new OleDbCommand();
  32. OleDbDataAdapter oda = new OleDbDataAdapter();
  33.  
  34. cmdExcel.Connection = connExcel;
  35.  
  36. connExcel.Open();
  37. DataTable dtExcelSchema;
  38. dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,null);
  39. string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
  40. connExcel.Close();
  41.  
  42. connExcel.Open();
  43. cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";
  44. oda.SelectCommand = cmdExcel;
  45. oda.Fill(dt);
  46. connExcel.Close();
  47. File.Delete(FilePath);
  48.  
  49. }
  50. }
  51. catch (Exception ex)
  52. {
  53.  
  54. }
  55. return dt;
  56. }

当使用第二个连接字符串时,我收到错误“外部表不是预期格式的connection.Open()”.但是当使用第一个时,我在阅读工作表名称时会收到错误.

请告诉我如何从Excel中读取表格或直接从Excel中读取数据.

解决方法

我认为这个 Third party dll-(ExcellDataReader)可能有助于解决你的问题.
  1. FileStream stream = File.Open(filePath,FileMode.Open,FileAccess.Read);
  2.  
  3. //1. Reading from a binary Excel file ('97-2003 format; *.xls)
  4. IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
  5. //...
  6. //2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
  7. IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
  8. //...
  9. //3. DataSet - The result of each spreadsheet will be created in the result.Tables
  10. DataSet result = excelReader.AsDataSet();
  11. //...
  12. //4. DataSet - Create column names from first row
  13. excelReader.IsFirstRowAsColumnNames = true;
  14. DataSet result = excelReader.AsDataSet();
  15.  
  16. //5. Data Reader methods
  17. while (excelReader.Read())
  18. {
  19. //excelReader.GetInt32(0);
  20. }
  21.  
  22. //6. Free resources (IExcelDataReader is IDisposable)
  23. excelReader.Close();

猜你在找的C#相关文章