zoukankan      html  css  js  c++  java
  • C#中如何将控件中表格数据导出到exeal文档中

    1、在项目的引入中,引入Microsoft.Office.Interop.Excel程序集,最好的11.0版本的。

    2、在源文件中的using 中,使用using Excel = Microsoft.Office.Interop.Excel;

    3、类似函数:


            /// <summary>
            /// 导出Excel的方法
            /// </summary>
            private void ExportExcel(DataGridView ds)
            {
                //DataSet ds = ;//数据源
                if (ds == null) return;

                string saveFileName = "";
                bool fileSaved = false;
                SaveFileDialog saveDialog = new SaveFileDialog();
                saveDialog.DefaultExt = "xls";
                saveDialog.Filter = "Excel|*.xls";
                saveDialog.FileName = "london";

              if (saveDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                {
                    return;
                }
                saveFileName = saveDialog.FileName;

                Excel.Application xlApp = new Excel.Application();

                if (xlApp == null)
                {
                    MessageBox.Show("未安装Excel");
                    return;
                }

                Excel.Workbooks workbooks = xlApp.Workbooks;
                Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
                Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Sheets[1];//取得sheet1

                int index = 0;
                List<string> listExcelColumns = new List<string>();
                //写入字段
                for (int i = 0; i < ds.Columns.Count; i++)
                {
                    if (ds.Columns[i].Visible)
                    {
                        worksheet.Cells[1, index + 1] = ds.Columns[i].HeaderText;
                        listExcelColumns.Add(ds.Columns[i].Name);
                        index++;
                    }
                }
                //写入数值

                for (int r = 0; r < ds.Rows.Count; r++)
                {
                    index = 0;
                    for (int i = 0; i < ds.Columns.Count; i++)
                    {
                        if (ds.Columns[i].Visible)
                        {
                            worksheet.Cells[r + 2, index + 1] = ds.Rows[r].Cells[i].Value;
                            index++;
                        }
                    }
                    System.Windows.Forms.Application.DoEvents();
                }
                worksheet.Columns.EntireColumn.AutoFit();//列宽自适应。

                int column = 0;
                column = listExcelColumns.IndexOf("UpdateTime") + 1;
                Excel.Range rg = worksheet.get_Range(worksheet.Cells[2, column], worksheet.Cells[ds.Rows.Count + 1, column]);//worksheet.Range[worksheet.Cells[2, column], worksheet.Cells[ds.Rows.Count + 1, column]];
                rg.NumberFormat = "yyyy-MM-dd HH:mm:ss";  // 日期时间列的自定义显示格式,默认的不显示秒

                if (saveFileName != "")
                {
                    try
                    {
                        workbook.Saved = true;
                        workbook.SaveCopyAs(saveFileName);
                        workbook.SaveAs(saveFileName,
                        fileSaved = true;
                    }
                    catch (Exception ex)
                    {
                        fileSaved = false;
                        MessageBox.Show("保存失败");
                    }
                }
                else
                {
                    fileSaved = false;
                }
                xlApp.Quit();
                GC.Collect();//强行销毁
                if (fileSaved && System.IO.File.Exists(saveFileName)) System.Diagnostics.Process.Start(saveFileName); //打开EXCEL
            }
        }

  • 相关阅读:
    实战分享 | 你知道这个死锁是怎么产生的吗?
    HDU 3016 线段树区间更新+spfa
    POJ 2828 线段树(想法)
    POJ 2184 01背包+负数处理
    HDU 2955 01背包(思维)
    HDU 1171 背包
    HDU 1561 树形DP入门
    POJ 3694 tarjan 桥+lca
    POJ 2446 最小点覆盖
    POJ 2226 最小点覆盖(经典建图)
  • 原文地址:https://www.cnblogs.com/XuYiHe/p/2110150.html
Copyright © 2011-2022 走看看