vs2005 操作excel 固定模板

本文介绍了如何使用VS2005和SQL2000开发工具,根据用户提供的固定模板生成报表。通过C#代码示例展示了如何打开Excel模板,填充数据并进行相关操作,涉及数据读取、Excel工作簿和工作表的操作等步骤。
摘要由CSDN通过智能技术生成

前一阵做了一个项目,我负责报表的生成。要求使用用户给定的固定模板,使用vs2005+sql2000 开发工具进行项目开发。本文只举一个例子。 下面代码 针对操作的模板以及数据库文档 见附件或图片。

 private void pb_print(string ID, DataTable certify)    //普板专用函数       调用版本
        {
            string strFileName, strsql, tempcells;
            string strsqlhead;
            DataSet dscertify;
            int i, j;
            int totalpage = 1, nowpage = 1, lastpage = 1, lastrecords = 0;
            strFileName ="D://普板专用.xlt";

            Microsoft.Office.Interop.Excel.Application myExcel;
            myExcel = new Microsoft.Office.Interop.Excel.Application();

            Microsoft.Office.Interop.Excel.Workbook myworkbook;
            Microsoft.Office.Interop.Excel.Worksheet xsheet;
            object missing = System.Reflection.Missing.Value;
            //*********打开模板**********************************************
            myworkbook = myExcel.Workbooks.Open(strFileName, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value);   //(strFileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);

            Microsoft.Office.Interop.Excel.Worksheet myworksheet = (Microsoft.Office.Interop.Excel.Worksheet)myworkbook.Sheets.get_Item(1);

            //固定打开代码
            //myExcel.Workbooks.Open(str, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
            //ThisApplication.Visible = true;
            myExcel.Visible = true;            //显示模板代码
            xsheet = (Microsoft.Office.Interop.Excel.Worksheet)myExcel.Worksheets.get_Item(1);

            strsql = "select NO as 序号, Grade as 钢级, BatchNo as 批号,HeatNo as 熔炼号, Thickness as 厚度, Width as 宽度, Longth as 长度, QTY as 数量,TONS as 重量,ComponentC as C,ComponentSi as Si,ComponentMn as Mn,ComponentP as P,ComponentS as S,ComponentAls as Als,ComponentCr as Cr,ComponentNi as Ni," +
                        "ComponentCu as Cu,ComponentNb as Nb,Ceq,YS as '屈服强度(Mpa)',TS as '抗拉强度(Mpa)',EL as 伸长率,SampleThickness as 试样厚度,Temperature as 温度,Impact1 as 冲击功1,Impact2 as 冲击功2,Impact3 as 冲击功3,StrainAgeingTemperature as 时效温度," +
                        "StrainAgeing1 as 时效1,StrainAgeing2 as 时效2,StrainAgeing3 as 时效3,CodeBendda as '冷弯d/a',CodeBendResualt as 冷弯结果,Fracture as 断口,UT as 超声波探伤UT,PRA1 as 断面收缩率1, PRA2 as 断面收缩率2,PRA3 as 断面收缩率3  ,ImpactDirection as 冲击功方向 ,CJGQKLX as 冲击功缺口 ,SXQKLX as 时效缺口类型 ,SXQYFX as 时效取样方向,ComponentMo as Mo from CertificateDetail where CertificateID =" + ID;
            myDataTable = new DataTable("cer");//

            certify = variableDeclare.dataBaceInterface.runQuery(strsql);
            dscertify = variableDeclare.dataBaceInterface.runQuery(strsql, "cer");//
            //dataGridView1.DataSource = dscertify;//
            //dataGridView1.DataMember = "cer";//
            DataRow dr;
            dr = certify.Rows[0];

            strsqlhead = "select * from Certificate where CertificateID =" + ID;
            DataTable cerhead = variableDeclare.dataBaceInterface.runQuery(strsqlhead);
            DataRow drhead;
            drhead = cerhead.Rows[0];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值