终端一体化运控平台
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

152 linhas
4.9 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.OleDb;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace BPASmartClient.Helper
  11. {
  12. public class ExcelHelper
  13. {
  14. public DataTable GetExcelToDataTableBySheet(string FileFullPath, string SheetName)
  15. {
  16. //此连接只能操作Excel2007之前(.xls)文件
  17. //string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + FileFullPath + ";Extended Properties='Excel 8.0; HDR=NO; IMEX=1'";
  18. //此连接可以操作.xls与.xlsx文件
  19. string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + FileFullPath + ";Extended Properties='Excel 12.0; HDR=NO; IMEX=1'";
  20. OleDbConnection conn = new OleDbConnection(strConn);
  21. conn.Open();
  22. DataSet ds = new DataSet();
  23. OleDbDataAdapter odda = new OleDbDataAdapter(string.Format("SELECT * FROM [{0}]", SheetName), conn);
  24. //OleDbDataAdapter odda = new OleDbDataAdapter(string.Format("select * from [Sheet1$]", conn),conn);                                   
  25. odda.Fill(ds, SheetName);
  26. conn.Close();
  27. return ds.Tables[0];
  28. }
  29. //根据Excel物理路径获取Excel文件中所有表名
  30. public String[] GetExcelSheetNames(string excelFile)
  31. {
  32. OleDbConnection objConn = null;
  33. System.Data.DataTable dt = null;
  34. try
  35. {
  36. //此连接只能操作Excel2007之前(.xls)文件
  37. //string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + excelFile + ";Extended Properties='Excel 8.0; HDR=NO; IMEX=1'";
  38. //此连接可以操作.xls与.xlsx文件
  39. string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + excelFile + ";Extended Properties='Excel 12.0; HDR=NO; IMEX=1'";
  40. objConn = new OleDbConnection(strConn);
  41. objConn.Open();
  42. dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
  43. if (dt == null)
  44. {
  45. return null;
  46. }
  47. String[] excelSheets = new String[dt.Rows.Count];
  48. int i = 0;
  49. foreach (DataRow row in dt.Rows)
  50. {
  51. excelSheets[i] = row["TABLE_NAME"].ToString();
  52. i++;
  53. }
  54. return excelSheets;
  55. }
  56. catch
  57. {
  58. return null;
  59. }
  60. finally
  61. {
  62. if (objConn != null)
  63. {
  64. objConn.Close();
  65. objConn.Dispose();
  66. }
  67. if (dt != null)
  68. {
  69. dt.Dispose();
  70. }
  71. }
  72. }
  73. public static DataTable ReadDataFromCSV( string file)
  74. {
  75. Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
  76. DataTable dt = null;
  77. if(File.Exists(file))
  78. {
  79. dt = new DataTable();
  80. FileStream fs = new FileStream(file, FileMode.Open,FileAccess.Read);
  81. StreamReader sr;
  82. try
  83. {
  84. Encoding encoding = Encoding.Default;
  85. using (sr = new StreamReader(fs, Encoding.GetEncoding("GB2312")))
  86. {
  87. string head = sr.ReadLine();
  88. string[] headNames = head.Split(',');
  89. for (int i = 0; i < headNames.Length; i++)
  90. {
  91. dt.Columns.Add(headNames[i], typeof(string));
  92. }
  93. while (!sr.EndOfStream)
  94. {
  95. string lineStr = sr.ReadLine();
  96. if (lineStr != null)
  97. {
  98. string[] lines = lineStr.Split(',');
  99. DataRow dr = dt.NewRow();
  100. for (int i = 0; i < lines.Length; i++)
  101. {
  102. dr[i] = lines[i];
  103. }
  104. dt.Rows.Add(dr);
  105. }
  106. }
  107. sr.Close();
  108. fs.Close();
  109. }
  110. }
  111. catch (Exception ex)
  112. {
  113. MessageLog.GetInstance.ShowEx($"读取csv文件异常:{ex.Message}");
  114. }
  115. }
  116. return dt;
  117. }
  118. }
  119. }