IniFileUtils类见本博客下,其他博客文章!!!https://www.yuque.com/yunaoya/ctc727/hcsbb7
记得在调用方法前加上:
EApp.DisplayAlerts = false;
EApp.ScreenUpdating = false;
方法调用
记得在调用方法后加上:
EApp.DisplayAlerts = true;
EApp.ScreenUpdating = true;
using System.IO;using Excel = Microsoft.Office.Interop.Excel;public Excel.Application EApp = new Excel.Application();public Excel.Workbook wb;public Excel.Worksheet ws;/// <summary>/// Excel导入数据到DataGridView/// </summary>/// <returns></returns>private bool GetDataFromExcel(){string storypath = IniFileUtils.ReadIni("ExcelInfo", "storyopenpath", "");if (string.IsNullOrEmpty(storypath)){storypath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);}System.Windows.Forms.OpenFileDialog opd = new System.Windows.Forms.OpenFileDialog{InitialDirectory = storypath,Title = "选择Excel文件:",Filter = "Excel2007+ files (*.xlsx)|*.xlsx",Multiselect = false};if (opd.ShowDialog() == DialogResult.OK){if (File.Exists(opd.FileName)){IniFileUtils.WriteIni("ExcelInfo", "storyopenpath", Path.GetDirectoryName(opd.FileName));wb = EApp.Workbooks.Open(opd.FileName);ws = wb.Worksheets["标牌"];int rcount = ws.Range["A" + ws.Range["A:A"].Rows.Count].End[Excel.XlDirection.xlUp].Row;int ccount = ((Excel.Range)ws.Cells[1, ws.Range["1:1"].Columns.Count].End[Excel.XlDirection.xlToLeft]).Column;for (int i = 2; i <= rcount; i++){if (AddDataView.RowCount <= i - 1){AddDataView.Rows.Insert(AddDataView.RowCount - 1);}for (int j = 1; j <= ccount; j++){if (ws.Cells[i, j].Value != null){Excel.Range rg = ws.Cells[i, j];string value = rg.Value.ToString();AddDataView.Rows[(i - 2)].Cells[(j - 1)].Value = value;}}}wb.Close();EApp.Quit();return true;}else{return false;}}else{return false;}}/// <summary>/// DataGridView导出数据到Excel/// </summary>/// <returns></returns>private bool OutDataOfDgv(){wb = EApp.Workbooks.Add();ws = wb.Worksheets[1];ws.Name = "标牌";ws.Range["A1:M1"].Value = new string[] { "指令号", "合同号", "产品型号", "产品图号", "铭牌图号", "ASME铭牌(复制铭牌)图号", "流程图牌图号", "液位表牌图号", "位号牌图号", "阀门标牌图号", "设计人", "送制日期", "备注" };string storypath = IniFileUtils.ReadIni("ExcelInfo", "storysavepath", "");if (string.IsNullOrEmpty(storypath)){storypath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);}System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog{InitialDirectory = storypath,FileName = $"{DateTime.Now.ToString("yy.MM.dd")} 标准表格 标牌打制.xlsx",Title = "选择Excel保存目录:",Filter = "Excel2007+ files (*.xlsx)|*.xlsx",};if (sfd.ShowDialog() == DialogResult.OK){if (Directory.Exists(Path.GetDirectoryName(sfd.FileName))){IniFileUtils.WriteIni("ExcelInfo", "storysavepath", Path.GetDirectoryName(sfd.FileName));int rcount = AddDataView.RowCount - 1;int ccount = AddDataView.ColumnCount;for (int i = 0; i < rcount; i++){for (int j = 0; j < ccount; j++){if (AddDataView.Rows[i].Cells[j].Value != null){Excel.Range rg = ws.Cells[i + 2, j + 1];rg.Value = AddDataView.Rows[i].Cells[j].Value.ToString();}}}//自动列宽ws.Cells.AutoFit();//设置边框ws.Range["A1:M" + ws.Range["A" + ws.Range["A:A"].Rows.Count].End[Excel.XlDirection.xlUp].Row].Borders.LineStyle = Excel.XlLineStyle.xlContinuous;wb.SaveAs(sfd.FileName);wb.Close();EApp.Quit();return true;}else{return false;}}else{return false;}}
