IniFileUtils类见本博客下,其他博客文章!!!https://www.yuque.com/yunaoya/ctc727/hcsbb7
    记得在调用方法前加上:
    EApp.DisplayAlerts = false;
    EApp.ScreenUpdating = false;

    方法调用
    记得在调用方法后加上:
    EApp.DisplayAlerts = true;
    EApp.ScreenUpdating = true;

    1. using System.IO;
    2. using Excel = Microsoft.Office.Interop.Excel;
    3. public Excel.Application EApp = new Excel.Application();
    4. public Excel.Workbook wb;
    5. public Excel.Worksheet ws;
    6. /// <summary>
    7. /// Excel导入数据到DataGridView
    8. /// </summary>
    9. /// <returns></returns>
    10. private bool GetDataFromExcel()
    11. {
    12. string storypath = IniFileUtils.ReadIni("ExcelInfo", "storyopenpath", "");
    13. if (string.IsNullOrEmpty(storypath))
    14. {
    15. storypath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
    16. }
    17. System.Windows.Forms.OpenFileDialog opd = new System.Windows.Forms.OpenFileDialog
    18. {
    19. InitialDirectory = storypath,
    20. Title = "选择Excel文件:",
    21. Filter = "Excel2007+ files (*.xlsx)|*.xlsx",
    22. Multiselect = false
    23. };
    24. if (opd.ShowDialog() == DialogResult.OK)
    25. {
    26. if (File.Exists(opd.FileName))
    27. {
    28. IniFileUtils.WriteIni("ExcelInfo", "storyopenpath", Path.GetDirectoryName(opd.FileName));
    29. wb = EApp.Workbooks.Open(opd.FileName);
    30. ws = wb.Worksheets["标牌"];
    31. int rcount = ws.Range["A" + ws.Range["A:A"].Rows.Count].End[Excel.XlDirection.xlUp].Row;
    32. int ccount = ((Excel.Range)ws.Cells[1, ws.Range["1:1"].Columns.Count].End[Excel.XlDirection.xlToLeft]).Column;
    33. for (int i = 2; i <= rcount; i++)
    34. {
    35. if (AddDataView.RowCount <= i - 1)
    36. {
    37. AddDataView.Rows.Insert(AddDataView.RowCount - 1);
    38. }
    39. for (int j = 1; j <= ccount; j++)
    40. {
    41. if (ws.Cells[i, j].Value != null)
    42. {
    43. Excel.Range rg = ws.Cells[i, j];
    44. string value = rg.Value.ToString();
    45. AddDataView.Rows[(i - 2)].Cells[(j - 1)].Value = value;
    46. }
    47. }
    48. }
    49. wb.Close();
    50. EApp.Quit();
    51. return true;
    52. }
    53. else
    54. {
    55. return false;
    56. }
    57. }
    58. else
    59. {
    60. return false;
    61. }
    62. }
    63. /// <summary>
    64. /// DataGridView导出数据到Excel
    65. /// </summary>
    66. /// <returns></returns>
    67. private bool OutDataOfDgv()
    68. {
    69. wb = EApp.Workbooks.Add();
    70. ws = wb.Worksheets[1];
    71. ws.Name = "标牌";
    72. ws.Range["A1:M1"].Value = new string[] { "指令号", "合同号", "产品型号", "产品图号", "铭牌图号", "ASME铭牌(复制铭牌)图号", "流程图牌图号", "液位表牌图号", "位号牌图号", "阀门标牌图号", "设计人", "送制日期", "备注" };
    73. string storypath = IniFileUtils.ReadIni("ExcelInfo", "storysavepath", "");
    74. if (string.IsNullOrEmpty(storypath))
    75. {
    76. storypath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
    77. }
    78. System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog
    79. {
    80. InitialDirectory = storypath,
    81. FileName = $"{DateTime.Now.ToString("yy.MM.dd")} 标准表格 标牌打制.xlsx",
    82. Title = "选择Excel保存目录:",
    83. Filter = "Excel2007+ files (*.xlsx)|*.xlsx",
    84. };
    85. if (sfd.ShowDialog() == DialogResult.OK)
    86. {
    87. if (Directory.Exists(Path.GetDirectoryName(sfd.FileName)))
    88. {
    89. IniFileUtils.WriteIni("ExcelInfo", "storysavepath", Path.GetDirectoryName(sfd.FileName));
    90. int rcount = AddDataView.RowCount - 1;
    91. int ccount = AddDataView.ColumnCount;
    92. for (int i = 0; i < rcount; i++)
    93. {
    94. for (int j = 0; j < ccount; j++)
    95. {
    96. if (AddDataView.Rows[i].Cells[j].Value != null)
    97. {
    98. Excel.Range rg = ws.Cells[i + 2, j + 1];
    99. rg.Value = AddDataView.Rows[i].Cells[j].Value.ToString();
    100. }
    101. }
    102. }
    103. //自动列宽
    104. ws.Cells.AutoFit();
    105. //设置边框
    106. ws.Range["A1:M" + ws.Range["A" + ws.Range["A:A"].Rows.Count].End[Excel.XlDirection.xlUp].Row].Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
    107. wb.SaveAs(sfd.FileName);
    108. wb.Close();
    109. EApp.Quit();
    110. return true;
    111. }
    112. else
    113. {
    114. return false;
    115. }
    116. }
    117. else
    118. {
    119. return false;
    120. }
    121. }