前一阵做了一个项目,我负责报表的生成。要求使用用户给定的固定模板,使用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];