导出数据到Excel模版并合并单元格

导出薪资模型至Excel
本文介绍了一种将薪资数据从应用程序导出到Excel的方法。通过读取模板文件并填充数据,实现薪资信息的批量导出及单元格合并。

        protected void btnExport_ServerClick(object sender, EventArgs e)
        {
            string strFileName = Server.MapPath(@"SalaryModel.xlsx");
            string strSaveFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + @".xls";
            Excel.Application ThisApplication = new Excel.Application();
            Excel.Workbook ThisWorkBook;
            object missing = System.Reflection.Missing.Value;

            try
            {
                //加载Excel模板文件
                ThisWorkBook = ThisApplication.Workbooks.Open(strFileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
                Excel.Worksheet ThisSheet = (Excel.Worksheet)ThisWorkBook.Sheets[1];
               
                ThisApplication.Visible = false;
                int nums;
                int j=0;
                int beforeRecords = 0;

                if (ds != null)
                {
                    if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                    {

                        for (int i = 0; i < RCount; i++)
                        {
                            #region 合并单元格
                            if (j < ds.Tables[1].Rows.Count)
                            {
                                if (i == beforeRecords)
                                {
                                    nums = Convert.ToInt32(ds.Tables[1].Rows[j]["projnums"].ToString());
                                    beforeRecords += nums;
                                    j++;

                                    Excel.Range range;
                                    range = ThisSheet.get_Range(ThisSheet.Cells[i + 6, 1], ThisSheet.Cells[i + 6 + nums - 1, 1]);
                                    range.MergeCells = true;
                                    range.set_Value(Type.Missing, j.ToString());

                                    range = ThisSheet.get_Range(ThisSheet.Cells[i + 6, 2], ThisSheet.Cells[i + 6 + nums - 1, 2]);
                                    range.MergeCells = true;
                                    range.set_Value(Type.Missing, ds.Tables[0].Rows[i]["UserName"].ToString());

                                    range = ThisSheet.get_Range(ThisSheet.Cells[i + 6, 3], ThisSheet.Cells[i + 6 + nums - 1, 3]);
                                    range.MergeCells = true;
                                    range.set_Value(Type.Missing, ds.Tables[0].Rows[i]["MonthlyTask"].ToString());

                                    range = ThisSheet.get_Range(ThisSheet.Cells[i + 6, 4], ThisSheet.Cells[i + 6 + nums - 1, 4]);
                                    range.MergeCells = true;
                                    range.set_Value(Type.Missing, ds.Tables[0].Rows[i]["AppOverTimeMoney"].ToString());

                                    range = ThisSheet.get_Range(ThisSheet.Cells[i + 6, 26], ThisSheet.Cells[i + 6 + nums - 1, 26]);
                                    range.MergeCells = true;
                                    range.set_Value(Type.Missing, ds.Tables[0].Rows[i]["Sumtotal"].ToString());

                                }
                            }
                            #endregion
                           
                            //ThisSheet.Cells[i + 6, 1] = j.ToString();
                            //ThisSheet.Cells[i + 6, 2] = ds.Tables[0].Rows[i]["UserName"].ToString();
                            //ThisSheet.Cells[i + 6, 3] = ds.Tables[0].Rows[i]["MonthlyTask"].ToString();
                            //ThisSheet.Cells[i + 6, 4] = ds.Tables[0].Rows[i]["AppOverTimeMoney"].ToString();

                            ThisSheet.Cells[i + 6, 5] = ds.Tables[0].Rows[i]["ProjectName"].ToString();
                            ThisSheet.Cells[i + 6, 6] = ds.Tables[0].Rows[i]["SetCostNum"].ToString();
                            ThisSheet.Cells[i + 6, 7] = ds.Tables[0].Rows[i]["SetOverNum"].ToString();
                            ThisSheet.Cells[i + 6, 8] = ds.Tables[0].Rows[i]["ContractMoney"].ToString();

                            ThisSheet.Cells[i + 6, 9] = ds.Tables[0].Rows[i]["OwnCourt"].ToString();
                            ThisSheet.Cells[i + 6, 10] = ds.Tables[0].Rows[i]["Outskirt"].ToString();
                            ThisSheet.Cells[i + 6, 11] = ds.Tables[0].Rows[i]["ServiceContract"].ToString();


                            ThisSheet.Cells[i + 6, 12] = ds.Tables[0].Rows[i]["sbys"].ToString();
                            ThisSheet.Cells[i + 6, 13] = ds.Tables[0].Rows[i]["gjfys"].ToString();
                            ThisSheet.Cells[i + 6, 14] = ds.Tables[0].Rows[i]["wwfys"].ToString();
                            ThisSheet.Cells[i + 6, 15] = ds.Tables[0].Rows[i]["rgfys"].ToString();
                            ThisSheet.Cells[i + 6, 16] = ds.Tables[0].Rows[i]["InnerBidBudget"].ToString();
                            ThisSheet.Cells[i + 6, 17] = ds.Tables[0].Rows[i]["sbjs"].ToString();
                            ThisSheet.Cells[i + 6, 18] = ds.Tables[0].Rows[i]["gjfjs"].ToString();
                            ThisSheet.Cells[i + 6, 19] = ds.Tables[0].Rows[i]["wwfjs"].ToString();
                            ThisSheet.Cells[i + 6, 20] = ds.Tables[0].Rows[i]["rgfjs"].ToString();
                            ThisSheet.Cells[i + 6, 21] = ds.Tables[0].Rows[i]["ProjectFee"].ToString();
                            ThisSheet.Cells[i + 6, 22] = ds.Tables[0].Rows[i]["ProjectReview"].ToString();
                            ThisSheet.Cells[i + 6, 23] = ds.Tables[0].Rows[i]["ExecutionContract"].ToString();
                            ThisSheet.Cells[i + 6, 24] = ds.Tables[0].Rows[i]["Visa"].ToString();

                            ThisSheet.Cells[i + 6, 25] = ds.Tables[0].Rows[i]["Subtotal"].ToString();
                            //ThisSheet.Cells[i + 6, 26] = ds.Tables[0].Rows[i]["Sumtotal"].ToString();
                        }
                    }
                }

                //更新数据后另存为新文件
                ThisSheet.SaveAs(strSaveFileName, missing, missing, missing, missing, missing, missing, missing, missing, missing);
            }
            catch(Exception ex) {
                Response.Write(ex.Message.ToString());
            }
            finally
            {
                ThisApplication.Quit();
                ThisWorkBook = null;
                ThisApplication = null;
            }
            try
            { //打开刚才生成的Excel文件
                Microsoft.Office.Interop.Excel.Workbook NewWorkBook;
                Microsoft.Office.Interop.Excel.Application NewApplication = new Microsoft.Office.Interop.Excel.ApplicationClass();
                NewWorkBook = NewApplication.Workbooks.Open(strSaveFileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
                Microsoft.Office.Interop.Excel.Worksheet NewSheet = (Microsoft.Office.Interop.Excel.Worksheet)NewWorkBook.Sheets[1];
                NewApplication.Visible = true;

                //也可以使用System.Diagnostics.Process.Start(strSaveFileName);来打开新文件

            }
            catch (Exception ex)
            {
                Response.Write(ex.Message.ToString());
            }
        }

转载于:https://www.cnblogs.com/wuquelin/articles/3089878.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值