当前位置:   article > 正文

OpenFileDialog选择文件并获取Excel数据/开窗选择保存路径_openfiledialog.filter选文件夹路径

openfiledialog.filter选文件夹路径
  1. public static System.Data.DataTable ExcelToDatatalbe(int startRow)//导入
  2. {
  3. OpenFileDialog ofd = new OpenFileDialog();
  4. ofd.Filter = "Excel文件|*.xls;*.xlsx";
  5. ofd.Title = "选择Excel文件";
  6. ofd.Multiselect = false;
  7. if (ofd.ShowDialog() == DialogResult.OK)
  8. {
  9. Aspose.Cells.Workbook book = new Aspose.Cells.Workbook();
  10. book.Open(ofd.FileName);
  11. Aspose.Cells.Worksheet sheet = book.Worksheets[0];
  12. Cells cells = sheet.Cells;
  13. //获取excel中的数据保存到一个datatable中
  14. System.Data.DataTable dt_Import = cells.ExportDataTableAsString(startRow, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, false);
  15. // dt_Import.
  16. return dt_Import;
  17. }
  18. else
  19. return new System.Data.DataTable();
  20. }
  1. /// <summary>
  2. /// 从Excel获取数据 NPOI
  3. /// </summary>
  4. /// <param name="startRow"></param>
  5. /// <returns></returns>
  6. public static System.Data.DataTable GetExcelData(int startRow, out string msg)
  7. {
  8. #region NPOI方式
  9. OpenFileDialog ofd = new OpenFileDialog();
  10. ofd.Filter = "Excel文件|*.xls;*.xlsx";
  11. ofd.Title = "选择Excel文件";
  12. ofd.Multiselect = false;
  13. msg = "";
  14. if (ofd.ShowDialog() == DialogResult.OK)
  15. {
  16. bool hasTitle = true;
  17. string fileName = ofd.FileName;
  18. Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
  19. Sheets sheets;
  20. object oMissiong = System.Reflection.Missing.Value;
  21. Microsoft.Office.Interop.Excel.Workbook workbook = null;
  22. System.Data.DataTable data = new System.Data.DataTable();
  23. try
  24. {
  25. if (app == null) return null;
  26. workbook = app.Workbooks.Open(fileName, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong,
  27. oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong);
  28. sheets = workbook.Worksheets;
  29. //将数据读入到DataTable中
  30. Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(1);//读取第一张表
  31. if (worksheet == null) return null;
  32. int iRowCount = worksheet.UsedRange.Rows.Count;
  33. int iColCount = worksheet.UsedRange.Columns.Count;
  34. //生成列头
  35. for (int i = 0; i < iColCount; i++)
  36. {
  37. var name = "column" + i;
  38. if (hasTitle)
  39. {
  40. var txt = ((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[startRow, i + 1]).Text.ToString();
  41. if (!string.IsNullOrEmpty(txt)) name = txt;
  42. }
  43. while (data.Columns.Contains(name)) name = name + "_1";//重复行名称会报错。
  44. data.Columns.Add(new DataColumn(name, typeof(string)));
  45. }
  46. //生成行数据
  47. Microsoft.Office.Interop.Excel.Range range;
  48. int rowIdx = hasTitle ? 2 : 1;
  49. for (int iRow = rowIdx; iRow <= iRowCount; iRow++)
  50. {
  51. DataRow dr = data.NewRow();
  52. for (int iCol = 1; iCol <= iColCount; iCol++)
  53. {
  54. range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[iRow, iCol];
  55. dr[iCol - 1] = (range.Value2 == null) ? "" : range.Text.ToString();
  56. }
  57. data.Rows.Add(dr);
  58. }
  59. }
  60. catch (Exception ex)
  61. {
  62. ExceptionUtil.DataCL_HTTP(ex, "", "", FileUtil.GetCurSourceFileName());
  63. msg = ex.Message;
  64. return null;
  65. }
  66. finally
  67. {
  68. workbook.Close(false, oMissiong, oMissiong);
  69. System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
  70. workbook = null;
  71. app.Workbooks.Close();
  72. app.Quit();
  73. System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
  74. app = null;
  75. }
  76. return data;
  77. }
  78. #endregion
  79. return null;
  80. }
  1. //选择文件夹
  2. FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
  3. if (folderBrowserDialog.ShowDialog(this) == DialogResult.OK)
  4. {
  5. string FolderName = folderBrowserDialog.SelectedPath;
  6. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/698864
推荐阅读
相关标签
  

闽ICP备14008679号