zoukankan      html  css  js  c++  java
  • datagridview 保存为excel输出

    private void btn_Out_Click(object sender, EventArgs e)
            {
                
                string fileName = "";
                string saveFileName = "";
                SaveFileDialog saveDialog = new SaveFileDialog();
                saveDialog.DefaultExt = "xls";
                saveDialog.Filter = "Excel文件|*.xls";
                saveDialog.FileName = fileName;
                saveDialog.ShowDialog();
                saveFileName = saveDialog.FileName;
                if (saveFileName.IndexOf(":") < 0) return; //被点了取消
                Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
                if (xlApp == null)
                {
                    MessageBox.Show("无法创建Excel对象,您的电脑可能未安装Excel");
                    return;
                }
                Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
                Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
                Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1 
                btn_Out.Enabled = false;
                btn_Out.Text = "请稍后";
                //写入标题             
                for (int i = 0; i < datagv_List.ColumnCount; i++)
                {
                    worksheet.Cells[1, i + 1] = datagv_List.Columns[i].HeaderText;
                }
                //写入数值
                for (int r = 0; r < datagv_List.Rows.Count; r++)
                {
                    for (int i = 0; i < datagv_List.ColumnCount; i++)
                    {
                        worksheet.Cells[r + 2, i + 1] = datagv_List.Rows[r].Cells[i].Value;
                    }
                    System.Windows.Forms.Application.DoEvents();
                }
                worksheet.Columns.EntireColumn.AutoFit();//列宽自适应
                MessageBox.Show( "保存成功", "提示", MessageBoxButtons.OK);
                if (saveFileName != "")
                {
                    try
                    {
                        workbook.Saved = true;
                        workbook.SaveCopyAs(saveFileName);  //fileSaved = true;                 
                    }
                    catch (Exception ex)
                    {//fileSaved = false;                      
                        MessageBox.Show("导出文件时出错,文件可能正被打开!
    " + ex.Message);
                    }
                }
                xlApp.Quit();
                GC.Collect();//强行销毁        
                btn_Out.Enabled = true;
                btn_Out.Text = "保存";
            }
  • 相关阅读:
    137. 只出现一次的数字 II
    JS_利用Canvas进行图片旋转
    JS_图片压缩并预览
    计蒜客——等和的分隔子集
    中缀表达式转后缀并计算(只考虑个位整数,不考虑除0等情况)
    求最小数 * 区间和最大值
    967 质量检测
    PAT-1102(Invert a Binary Tree)
    PAT-1100(Mars Numbers)
    PAT-1099(Build A Binary Search Tree)
  • 原文地址:https://www.cnblogs.com/zzx849918265/p/12943031.html
Copyright © 2011-2022 走看看