终端一体化运控平台
Não pode escolher mais do que 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.

ExcelHelper.cs 4.9 KiB

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