导出excel调用模版

 //导出方法
        void ExportExcelMethod(object param)
        {
            if (ObsTB.Count < 1)
            {
                MessageBox.Show("没有要导出的数据");
                return;
            }
            Excel.Application objApp = new Excel.Application(); //实例化Excle对象
            if (objApp == null)
            {
                MessageBox.Show("无法创建Excel对象,可能计算机未安装Excel!");
                return;
            }
 
            string path = Invoice.Utils.Validation.AppPath();
            objApp.Workbooks.Add(path + @"Excel/发票使用情况一览表.xls");
 
            Excel.Sheets objSheets = objApp.Worksheets;//实例化sheet工作表的集合
            Excel._Worksheet objSheet;
            objSheet = (Excel._Worksheet)objSheets.get_Item(1);//导出sheet1模板
            objSheet.Name = "发票使用情况一览表";
            objApp.Workbooks[1].SaveCopyAs("C:/ceshi.xls");
            objApp.Workbooks[1].BeforeClose += new Excel.WorkbookEvents_BeforeCloseEventHandler(VMFPHeadQuery_BeforeClose);
             
            //设置表头名称
            objApp.Cells[1, 1] = "发票使用情况一览表";
            //设置数据行的格式边框
            //objApp.get_Range((object)objApp.Cells[1, 1], (object)objApp.Cells[obsTB.Count + 3, 22]).Borders.LineStyle = 1;
            objApp.get_Range((object)objApp.Cells[1, 1], (object)objApp.Cells[1, 22]).Interior.ColorIndex = 2;
            //设置标题字体大小
            objApp.get_Range((object)objApp.Cells[1, 1], (object)objApp.Cells[1, 22]).Font.Size = 18;
            //设置内容字体大小
            objApp.get_Range((object)objApp.Cells[4, 1], (object)objApp.Cells[this.ObsTB.Count + 3, 22]).Font.Size = 10;
 
            //string userid = "  操作员编:" + App.OperationUserID.ToString();
            string username = " 操作员:" + App.OperationUserName.ToString();
            //objApp.Cells[2, 1] = "生成时间:" + DateTime.Now.Year + "年" + DateTime.Now.Month + "月" + DateTime.Now.Day + "日" + userid + username;
            objSheet.PageSetup.RightFooter = "生成时间:" + DateTime.Now.Year + "年" + DateTime.Now.Month + "月" + DateTime.Now.Day + "日"+ username;
            //行列
            int iRowIndex = 0;
            int iColIndex = 0;
            int iRowCount = ObsTB.Count;
            int iColCount = 22;
            //二维数组存放数据
            object[,] objData = new object[iRowCount+1, iColCount];
            int j = 1;
            for (iRowIndex = 0; iRowIndex < iRowCount; iRowIndex++)
            {
                for (iColIndex = 0; iColIndex < iColCount; iColIndex++)
                {
                   if(iColIndex==0)
                       objData[iRowIndex, iColIndex] = j++; 
                   if (iColIndex == 1)
                    objData[iRowIndex, iColIndex] = UtilsConverter.GetPJtypNameByPJtyp(ObsTB[iRowIndex].Pjtype);
                   if (iColIndex == 2)
                       objData[iRowIndex, iColIndex] = ObsTB[iRowIndex].Fptype;
                   if (iColIndex == 3)
                       objData[iRowIndex, iColIndex] = "'" + ObsTB[iRowIndex].Dhp;
                   if (iColIndex == 4)
                       objData[iRowIndex, iColIndex] = "'" + ObsTB[iRowIndex].Fpconde;
                   if (iColIndex == 5)
                       objData[iRowIndex, iColIndex] = "'" + ObsTB[iRowIndex].Fpnum;
                   if (iColIndex == 6)
                       objData[iRowIndex, iColIndex] = UtilsConverter.GetPiPeiNameBykjzt(ObsTB[iRowIndex].Status1);
                   if (iColIndex == 7)
                       objData[iRowIndex, iColIndex] = UtilsConverter.GetPrintZTNameBykjzt(ObsTB[iRowIndex].Status2);
                   if (iColIndex == 8)
                       objData[iRowIndex, iColIndex] = UtilsConverter.GetKJZTNameBykjzt(ObsTB[iRowIndex ].Status3);
                   if (iColIndex == 9)
                       objData[iRowIndex, iColIndex] = "'" + ObsTB[iRowIndex].Oper3;
                   if (iColIndex == 10)
                       objData[iRowIndex, iColIndex] = "'" + ObsTB[iRowIndex].Gentime3;
                   if (iColIndex == 11)
                       objData[iRowIndex, iColIndex] = UtilsConverter.GetZFZTNameByzfzt(ObsTB[iRowIndex].Status4);
                   if (iColIndex == 12)
                       objData[iRowIndex, iColIndex] = "'" + ObsTB[iRowIndex].Oper4;
                   if (iColIndex == 13)
                       objData[iRowIndex, iColIndex] = "'" + ObsTB[iRowIndex].Gentime4;
                   if (iColIndex == 14)
                       objData[iRowIndex, iColIndex] = UtilsConverter.GetPDZTNameByPdzt(ObsTB[iRowIndex].Status5);
                   if (iColIndex == 15)
                       objData[iRowIndex, iColIndex] = "'" + ObsTB[iRowIndex].Oper5;
                   if (iColIndex == 16)
                       objData[iRowIndex, iColIndex] = "'" + ObsTB[iRowIndex].Gentime5;
                   if (iColIndex == 17)
                       objData[iRowIndex, iColIndex] = UtilsConverter.GetJHZTNameByjhzt(ObsTB[iRowIndex].Status6);
                   if (iColIndex == 18)
                       objData[iRowIndex, iColIndex] = UtilsConverter.GetHXZTNameByzfzt(ObsTB[iRowIndex].Status7);
                   if (iColIndex == 19)
                       objData[iRowIndex, iColIndex] = "'" + ObsTB[iRowIndex].Oper7;
                   if (iColIndex == 20)
                       objData[iRowIndex, iColIndex] = "'" + ObsTB[iRowIndex].Gentime7;
                   if (iColIndex == 21)
                       objData[iRowIndex, iColIndex] = "'" + ObsTB[iRowIndex].Bz;
                }
            }
            Excel.Range range;
            range = objSheet.get_Range((object)objApp.Cells[4, 1], (object)objApp.Cells[iRowCount+3, 22]);
            range.Value2 = objData;
           
            objApp.Visible = true;
            objApp.DisplayAlerts = false;
            Print(objApp, objSheet);
           
            GC.Collect();
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值