• Winform 导出Excel


     private void 导出excelToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (dgvData.RowCount > 0)
                {
                    var saveFileDialog1 = new SaveFileDialog
                    {
                        Filter = @"Excel文件(*.xls,*.xlsx)|*.xls;*.xlsx",
                        RestoreDirectory = true
                    };
    
                    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        var result = _excel.SetExportExcel(saveFileDialog1.FileName, dgvData, 2);
                        if (result != null)
                        {
                            MessageBox.Show(result.HasError ? "文件导出成功!!" : result.ErrorMessage);
                        }
                    }
                }
                else
                {
                    MessageBox.Show(@"数据为空!!");
                }
            }
    
            /// <summary>
            /// DataGridView 导出Excel方法
            /// </summary>
            /// <param name="fileName">导出路径</param>
            /// <param name="gridView">数据源</param>
            /// <param name="num">从第几列开始导</param>
            public Result SetExportExcel(string fileName, DataGridView gridView, int num)
            {
                var result = new Result();
    
                if (fileName.IndexOf(":") > 0)
                {
                    Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
    
                    if (xlApp == null)
                    {
                        result.ErrorMessage = "无法创建Excel对象,可能您的机子未安装Excel";
                        result.HasError = false;
                    }
                    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
    
                    int a = 1, b = 1;
                    for (var i = 0; i < gridView.RowCount; i++)
                    {
                        if (i == 0)
                        {
                            for (var j = num; j < gridView.Columns.Count; j++)
                            {
                                worksheet.Cells[1, a] = gridView.Columns[j].HeaderText;
                                a++;
                            }
                        }
                        for (var j = num; j < gridView.Columns.Count; j++)
                        {
                            worksheet.Cells[i + 2, b] = gridView.Rows[i].Cells[j].Value;
                            b++;
                        }
                        b = 1;
                        System.Windows.Forms.Application.DoEvents();
                    }
    
                    worksheet.Columns.EntireColumn.AutoFit(); //列宽自适应
                    if (fileName != "")
                    {
                        try
                        {
                            workbook.Saved = true;
                            workbook.SaveCopyAs(fileName);
                        }
                        catch (Exception ex)
                        {
                            result.ErrorMessage = "文件导出时出错,文件可能正被打开!" + ex.Message;
                            result.HasError = false;
                        }
                    }
                    xlApp.Quit();
                    GC.Collect(); //强行销毁
                    result.HasError = true;
                }
                return result;
            }
    
            /// <summary>
            /// ListView 导出Excel方法
            /// </summary>
            /// <param name="fileName">导出路径</param>
            /// <param name="listView">数据源</param>
            /// <param name="num">从第几列开始导</param>
            public Result SetListViewExportExcel(string fileName, ListView listView, int num)
            {
                var result = new Result();
    
                if (fileName.IndexOf(":") > 0)
                {
                    Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
    
                    if (xlApp == null)
                    {
                        result.ErrorMessage = "无法创建Excel对象,可能您的机子未安装Excel";
                        result.HasError = false;
                    }
                    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
    
                    int a = 1, b = 1;
                    for (var i = 0; i < listView.Items.Count; i++)
                    {
                        if (i == 0)
                        {
                            for (var j = num; j < listView.Columns.Count; j++)
                            {
                                worksheet.Cells[1, a] = listView.Columns[j].Text;
                                a++;
                            }
                        }
                        for (var j = num; j < listView.Columns.Count; j++)
                        {
                            worksheet.Cells[i + 2, b] = listView.Items[i].SubItems[j].Text;
                            b++;
                        }
                        b = 1;
                        System.Windows.Forms.Application.DoEvents();
                    }
    
                    worksheet.Columns.EntireColumn.AutoFit(); //列宽自适应
                    if (fileName != "")
                    {
                        try
                        {
                            workbook.Saved = true;
                            workbook.SaveCopyAs(fileName);
                        }
                        catch (Exception ex)
                        {
                            result.ErrorMessage = "文件导出时出错,文件可能正被打开!" + ex.Message;
                            result.HasError = false;
                        }
                    }
                    xlApp.Quit();
                    GC.Collect(); //强行销毁
                    result.HasError = true;
                }
                return result;
            }
  • 相关阅读:
    使用BeanShell 对比取出来的值
    https 请求的端口是443 注意
    Jmeter录制App 请求是HTTPS的
    Charles-断点
    随手记--分配事件概率
    看日志有没有 出现错误的字段 (如 crash ) 查找app闪退
    学习的网站
    xss 攻击 sql 注入
    app的apk 安装的方法--adb--命令安装 (含把apk放某个文件夹,每次启动自己安装)
    把2列相加的方法
  • 原文地址:https://www.cnblogs.com/xiaoqi742709106/p/4643123.html
Copyright © 2020-2023  润新知