锐腾搅拌上料功能
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.8 KiB

5 months ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.OleDb;
  5. using System.Windows.Forms;
  6. using DevExpress.XtraEditors;
  7. using ICSSoft.Base.Config.AppConfig;
  8. namespace ICSSoft.Frame.APP
  9. {
  10. public partial class FormReadExcel : XtraForm
  11. {
  12. public DataTable _excelData = null;
  13. public string _url = "";
  14. public FormReadExcel()
  15. {
  16. InitializeComponent();
  17. }
  18. private void txtUrl_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
  19. {
  20. OpenFileDialog of = new OpenFileDialog {Filter = @"excel|*.xls;*.xlsx"};
  21. if (of.ShowDialog() == DialogResult.OK)
  22. {
  23. txtUrl.Text = of.FileName;
  24. txtSheet.Properties.Items.Clear();
  25. txtSheet.Text = "";
  26. string[] strs = GetExcelSheetNames(of.FileName);
  27. foreach (string str in strs)
  28. {
  29. txtSheet.Properties.Items.Add(str);
  30. }
  31. }
  32. }
  33. private void btnOk_Click(object sender, EventArgs e)
  34. {
  35. if (txtUrl.Text.Trim() == "")
  36. return;
  37. if (txtSheet.Text.Trim() == "")
  38. return;
  39. _excelData = AppConfig.GetExcelData(txtUrl.Text, txtSheet.Text.Trim());
  40. _url = txtUrl.Text;
  41. DialogResult = DialogResult.OK;
  42. }
  43. private static string[] GetExcelSheetNames(string fileName)
  44. {
  45. OleDbConnection objConn = null;
  46. System.Data.DataTable dt = null;
  47. try
  48. {
  49. string connString;
  50. string fileType = fileName.Substring(fileName.LastIndexOf("."));
  51. if (fileType == ".xls")
  52. connString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
  53. "Data Source=" + fileName + ";Extended Properties=Excel 8.0;";
  54. else//.xlsx
  55. connString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + fileName + ";" + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\"";
  56. // 创建连接对象
  57. objConn = new OleDbConnection(connString);
  58. // 打开数据库连接
  59. objConn.Open();
  60. // 得到包含数据架构的数据表
  61. dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
  62. if (dt == null)
  63. {
  64. return null;
  65. }
  66. List<string> excelSheets = new List<string>();
  67. // 添加工作表名称到字符串数组
  68. foreach (DataRow row in dt.Rows)
  69. {
  70. string strSheetTableName = row["TABLE_NAME"].ToString();
  71. if (strSheetTableName.EndsWith("'"))
  72. {
  73. strSheetTableName = strSheetTableName.Substring(1, strSheetTableName.Length - 2);
  74. }
  75. //过滤无效SheetName
  76. if (strSheetTableName.Contains("$") && strSheetTableName.Replace("'", "").EndsWith("$"))
  77. {
  78. excelSheets.Add(strSheetTableName.Substring(0, strSheetTableName.Length - 1));
  79. }
  80. }
  81. return excelSheets.ToArray();
  82. }
  83. catch (Exception ex)
  84. {
  85. MessageBox.Show(ex.ToString());
  86. return null;
  87. }
  88. finally
  89. {
  90. // 清理
  91. if (objConn != null)
  92. {
  93. objConn.Close();
  94. objConn.Dispose();
  95. }
  96. if (dt != null)
  97. {
  98. dt.Dispose();
  99. }
  100. }
  101. }
  102. }
  103. }