CNAS取数仪器端升级
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

hace 4 meses
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data;
  6. using System.Data.OleDb;
  7. using CnasSynchronousCommon;
  8. namespace CnasSynchronusDAL
  9. {
  10. public static class ExcelDAL
  11. {
  12. /// <summary>
  13. /// 读取Excel第一个工作表的内容
  14. /// </summary>
  15. /// <param name="strPath"></param>
  16. /// <returns></returns>
  17. public static DataTable ReadExcelFirstSheetToTable(string strPath, string strExcelFileVersion)
  18. {
  19. DataTable dt = new DataTable();
  20. try
  21. {
  22. //连接字符串
  23. //string connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strPath + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';"; // Office 07及以上版本 不能出现多余的空格 而且分号注意
  24. string connstring = "Provider = Microsoft.JET.OLEDB.4.0; Data Source = " + strPath + "; Extended Properties = 'Excel 8.0;HDR=NO;IMEX=1'; "; //Office 07以下版本
  25. if (!string.IsNullOrWhiteSpace(strExcelFileVersion) && strExcelFileVersion != "0")
  26. connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strPath + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';"; // Office 07及以上版本 不能出现多余的空格 而且分号注意
  27. using (OleDbConnection conn = new OleDbConnection(connstring))
  28. {
  29. conn.Open();
  30. DataTable sheetsName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" }); //得到所有sheet的名字
  31. string firstSheetName = sheetsName.Rows[0][2].ToString(); //得到第一个sheet的名字
  32. string sql = string.Format("SELECT * FROM [{0}]", firstSheetName); //查询字符串
  33. //string sql = string.Format("SELECT * FROM [{0}] WHERE [日期] is not null", firstSheetName); //查询字符串
  34. OleDbDataAdapter ada = new OleDbDataAdapter(sql, connstring);
  35. DataSet set = new DataSet();
  36. ada.Fill(set);
  37. return set.Tables[0];
  38. }
  39. }
  40. catch(Exception ex)
  41. {
  42. //发生异常,写入日志
  43. AppLog.Error(ex.Message);
  44. }
  45. return dt;
  46. }
  47. internal static DataTable ReadExcelTableStruct(string strPath, string strExcelFileVersion,string strViewName,string strViewSQL, string strTableName)
  48. {
  49. DataTable dtNormal = new DataTable();
  50. try
  51. {
  52. //连接字符串
  53. string connstring = "Provider = Microsoft.JET.OLEDB.4.0; Data Source = " + strPath + "; Extended Properties = 'Excel 8.0;HDR=NO;IMEX=1'; "; //Office 07以下版本
  54. if (!string.IsNullOrWhiteSpace(strExcelFileVersion) && strExcelFileVersion != "0")
  55. connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strPath + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';"; // Office 07及以上版本 不能出现多余的空格 而且分号注意
  56. using (OleDbConnection conn = new OleDbConnection(connstring))
  57. {
  58. conn.Open();
  59. DataTable sheetsName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" }); //得到所有sheet的名字
  60. string sql = "";
  61. OleDbDataAdapter ada = null;
  62. if (strViewName == strTableName && !string.IsNullOrWhiteSpace(strViewName))
  63. sql = strViewSQL + " where 1=0";
  64. else
  65. sql = string.Format("SELECT * FROM [{0}$] where 1=0", strTableName); //查询字符串
  66. ada = new OleDbDataAdapter(sql, connstring);
  67. DataSet set = new DataSet();
  68. ada.Fill(set);
  69. dtNormal = set.Tables[0];
  70. }
  71. }
  72. catch (Exception ex)
  73. {
  74. //发生异常,写入日志
  75. AppLog.Error(ex.Message);
  76. }
  77. return dtNormal;
  78. }
  79. /// <summary>
  80. /// 通过表名读取Excel中数据
  81. /// </summary>
  82. /// <param name="strPath"></param>
  83. /// <param name="strTableName"></param>
  84. /// <returns></returns>
  85. public static DataTable ReadExcelByNameToTable(string strPath, string strExcelFileVersion, string strViewName,string strViewSQL,string strTableName)
  86. {
  87. DataTable dtNormal = new DataTable();
  88. try
  89. {
  90. //连接字符串
  91. string connstring = "Provider = Microsoft.JET.OLEDB.4.0; Data Source = " + strPath + "; Extended Properties = 'Excel 8.0;HDR=NO;IMEX=1'; "; //Office 07以下版本
  92. if (!string.IsNullOrWhiteSpace(strExcelFileVersion) && strExcelFileVersion != "0")
  93. connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strPath + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';"; // Office 07及以上版本 不能出现多余的空格 而且分号注意
  94. using (OleDbConnection conn = new OleDbConnection(connstring))
  95. {
  96. conn.Open();
  97. DataTable sheetsName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" }); //得到所有sheet的名字
  98. string sql = "";
  99. OleDbDataAdapter ada = null;
  100. if (strViewName == strTableName && !string.IsNullOrWhiteSpace(strViewName))
  101. sql = strViewSQL;
  102. else
  103. sql = string.Format("SELECT * FROM [{0}$]", strTableName); //查询字符串
  104. ada = new OleDbDataAdapter(sql, connstring);
  105. DataSet set = new DataSet();
  106. ada.Fill(set);
  107. DataTable dtSheetData = set.Tables[0];
  108. DataRow drCurrent = dtSheetData.Rows[0];
  109. //创建标准表结构
  110. foreach (DataColumn dc in dtSheetData.Columns)
  111. {
  112. DataColumn dcReady = new DataColumn
  113. {
  114. DataType = dc.DataType,
  115. ColumnName = drCurrent[dc.ColumnName].ToString()
  116. };
  117. dtNormal.Columns.Add(dcReady);
  118. }
  119. //向列表中添加数据
  120. for (int i = 1; i < dtSheetData.Rows.Count; i++)
  121. {
  122. DataRow drNew = dtNormal.NewRow();
  123. for (int j = 0; j < dtSheetData.Columns.Count; j++)
  124. {
  125. drNew[j] = dtSheetData.Rows[i][j];
  126. }
  127. dtNormal.Rows.Add(drNew);
  128. }
  129. }
  130. }
  131. catch (Exception ex)
  132. {
  133. //发生异常,写入日志
  134. AppLog.Error(ex.Message);
  135. }
  136. return dtNormal;
  137. }
  138. /// <summary>
  139. /// 读取Excel中所有表的表名
  140. /// </summary>
  141. /// <param name="strPath"></param>
  142. /// <returns></returns>
  143. public static Dictionary<string, DataTable> ReadExcelTableNameToTable(string strPath,string strExcelFileVersion)
  144. {
  145. Dictionary<string, DataTable> dictTables = new Dictionary<string, DataTable>();
  146. try
  147. {
  148. //连接字符串
  149. string connstring = "Provider = Microsoft.JET.OLEDB.4.0; Data Source = " + strPath + "; Extended Properties = 'Excel 8.0;HDR=NO;IMEX=1'; "; //Office 07以下版本
  150. if (!string.IsNullOrWhiteSpace(strExcelFileVersion) && strExcelFileVersion != "0")
  151. connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strPath + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';"; // Office 07及以上版本 不能出现多余的空格 而且分号注意
  152. using (OleDbConnection conn = new OleDbConnection(connstring))
  153. {
  154. conn.Open();
  155. DataTable sheetsName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" }); //得到所有sheet的名字
  156. string sql = "";
  157. OleDbDataAdapter ada = null;
  158. foreach (DataRow dr in sheetsName.Rows)
  159. {
  160. string strTableName = dr[2].ToString();
  161. sql = string.Format("SELECT * FROM [{0}]", strTableName); //查询字符串
  162. ada = new OleDbDataAdapter(sql, connstring);
  163. DataSet set = new DataSet();
  164. ada.Fill(set);
  165. //DataTable dtSheetData = set.Tables[0];
  166. //DataTable dtNormal = new DataTable();
  167. //DataRow drCurrent = dtSheetData.Rows[0];
  168. ////创建标准表结构
  169. //foreach (DataColumn dc in dtSheetData.Columns)
  170. //{
  171. // DataColumn dcReady = new DataColumn
  172. // {
  173. // DataType = dc.DataType,
  174. // ColumnName = drCurrent[dc.ColumnName].ToString()
  175. // };
  176. // dtNormal.Columns.Add(dcReady);
  177. //}
  178. //dictTables.Add(strTableName.ToUpper(), set.Tables[0]);
  179. dictTables.Add(strTableName.ToUpper(), set.Tables[0]);
  180. }
  181. }
  182. }
  183. catch(Exception ex)
  184. {
  185. //发生异常,写入日志
  186. AppLog.Error(ex.Message);
  187. }
  188. return dictTables;
  189. }
  190. public static DataTable ReadExcelTableNameToTableByDate(string strPath,string strExcelFileVersion,string strViewName,string strViewSQL,string strTableName,string strDateColumn,string strDate)
  191. {
  192. DataTable dtNormal = new DataTable();
  193. try
  194. {
  195. //连接字符串
  196. //string connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strPath + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';"; // Office 07及以上版本 不能出现多余的空格 而且分号注意
  197. string connstring = "Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + strPath + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';"; //Office 07以下版本
  198. if (!string.IsNullOrWhiteSpace(strExcelFileVersion) && strExcelFileVersion != "0")
  199. connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strPath + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';"; // Office 07及以上版本 不能出现多余的空格 而且分号注意
  200. using (OleDbConnection conn = new OleDbConnection(connstring))
  201. {
  202. conn.Open();
  203. DataTable sheetsName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" }); //得到所有sheet的名字
  204. string sql = "";
  205. OleDbDataAdapter ada = null;
  206. if (strViewName == strTableName && !string.IsNullOrWhiteSpace(strViewName))
  207. sql = strViewSQL;
  208. else
  209. sql = string.Format("SELECT * FROM [{0}]", strTableName); //查询字符串
  210. ada = new OleDbDataAdapter(sql, connstring);
  211. DataSet set = new DataSet();
  212. ada.Fill(set);
  213. dtNormal = set.Tables[0].Clone();
  214. if (set.Tables[0].Rows.Count > 0)
  215. {
  216. DataRow[] rows = set.Tables[0].Select(string.Format("{0}>'{1}'", strDateColumn, strDate));
  217. foreach (DataRow dr in rows)
  218. {
  219. DataRow drNew = dtNormal.NewRow();
  220. drNew.ItemArray = dr.ItemArray;
  221. dtNormal.Rows.Add(drNew);
  222. }
  223. }
  224. }
  225. }
  226. catch (Exception ex)
  227. {
  228. //发生异常,写入日志
  229. AppLog.Error(ex.Message);
  230. }
  231. return dtNormal;
  232. }
  233. }
  234. }