c#导出excel

c#导出excel其实很简单,不管导出的数据存在dataGridView还是dataTable 中,都可以用这里的模板。只要稍微修改一下就行(此处使用了模板复制的方法,即层一个模板复制数据,然后往单元格填充数据)

此处以DataGridView为例:

1、

/// <summary>
        /// 导出excel执行方法
        /// </summary>
        /// <param name="dgv"></param>
        private void outLoadExcel(DataGridView dgv)
        {

            try
            {
                if (dgv.Rows.Count > 0)
                {
                    FolderBrowserDialog openFileDialog = new FolderBrowserDialog();
                    openFileDialog.ShowDialog();
                    if (string.IsNullOrEmpty(openFileDialog.SelectedPath))
                    {
                        return;
                    }
                    fixScanService.ExportExcel(openFileDialog.SelectedPath, dgv, "");
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {}
        }
2、

/// <summary>
        /// service导出维修日报表档案
        /// </summary>
        /// <param name="selectedPath"></param>
        /// <param name="ListOrgFile"></param>
        /// <param name="fileName"></param>
        public void ExportExcel(string selectedPath, DataGridView dgv, string fileName)
        {

            Excel.Application application = null;
            Excel.Workbook workBook = null;
            string outFileName = "";
            try
            {
                object Nothing = System.Reflection.Missing.Value;
                application = new Excel.Application();
                Excel.Workbooks workbooks = (Excel.Workbooks)application.Workbooks;
                workbooks.Open(System.AppDomain.CurrentDomain.BaseDirectory + @"Template\" + "维修日报表模板.xlsx", Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing);//这里取得是Template下的模板,维修日报表模板.xlsx

                workBook = workbooks.get_Item(1);
                Excel.Worksheet ws_template = workBook.Worksheets.get_Item(2) as Excel.Worksheet;
                Excel.Worksheet ws_target = workBook.Worksheets.get_Item(1) as Excel.Worksheet;
                if (ExportExcel_Sub(ws_template, ws_target, dgv))//模板复制
                {
                    workBook.Saved = true;
                    outFileName = selectedPath + "\\" + Path.GetFileNameWithoutExtension(fileName) + "维修日报表.xlsx";
                    application.ActiveWorkbook.SaveAs(outFileName, Nothing, Nothing, Nothing, Nothing, Nothing, 0, Nothing, Nothing, Nothing, Nothing, 0);
                    application.DisplayAlerts = false; //删除第二页
                    ((Excel.Worksheet)workBook.Worksheets[2]).Delete();
                    application.DisplayAlerts = true;

                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (workBook != null)
                {
                    workBook.Close(true, Type.Missing, Type.Missing);
                    workBook = null;
                }
                if (application != null)
                {
                    application.Quit();
                    ExeclTool.Kill(application);
                    application = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            if (File.Exists(outFileName))
            {
                System.Diagnostics.Process.Start(outFileName);
            }
        }

3、

 /// <summary>
        /// 模板复制
        /// </summary>
        /// <param name="ws_template"></param>
        /// <param name="ws_target"></param>
        /// <param name="dgv"></param>
        /// <returns></returns>
        public bool ExportExcel_Sub(Excel.Worksheet ws_template, Excel.Worksheet ws_target, DataGridView dgv)
        {

            bool b_return = false;
            Excel.Range range;
            Excel.Range range1;
            int row_all = 0;
            //填充内容
            for (int i = 0; i < dgv.Rows.Count; i++)
            {

                b_return = true;
                range = ws_template.get_Range(ws_template.Cells[3, 1], ws_template.Cells[3, 20]);A3--Z3?
                range1 = ws_target.get_Range(ws_target.Cells[i+3, 1], ws_target.Cells[i+3, 50]);
                //指定为文本格式
                range.NumberFormatLocal = "@";
                range.Copy(range1);

                ws_target.Cells[i+3, 1] = i+1;

                for (int j = 2; j < 12;j++ ) {
                    if (dgv[j, row_all].Value == null)
                    {
                        ws_target.Cells[i+3, j] = "";
                    }
                    else {
                        ws_target.Cells[i+3, j] = dgv[j, row_all].Value.ToString();
                    }                   
                }               
                row_all++;
            }           
            range1 = null;
            range = null;
            return b_return;
        }

如果你直接复制过来,肯定有很多错,只要稍微修改一下就好了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值