首页 > 开发 > 综合 > 正文

C#下面调用 Excel 打印代码

2024-07-21 02:25:50
字体:
来源:转载
供稿:网友
国内最大的酷站演示中心!

private void btnprintdata_click(object sender, eventargs e)
        {
            excel.application xlsapp = new excel.application();
            excel._workbook xlsbook = xlsapp.workbooks.add(excel.xlwbatemplate.xlwbatworksheet);
            excel._worksheet xlssheet = (excel._worksheet)xlsbook.worksheets[1];
            xlssheet.activate();

            xlsapp.caption = lbldmmc.text + "断面实测成果表 - 打印预览 ";
            datatable mydt1,mydt2;
         
            mydt1 = datatable(a,a1);
            mydt2 = datatable(a,a1);
          
            string[] strcolname = new string[] { "起点距m", "河底高程m", "起点距m", "河底高程m", "起点距m", "河底高程m", "起点距m", "河底高程m", "起点距m", "河底高程m", "起点距m", "河底高程m", "" };
            int intcols = strcolname.length - 1;
            int introws = mydt1.rows.count;
        
           // int[] introws1 = new int[mydt1.rows.count];
          //  int[] intcols1 = new int[mydt1.columns.count];

            int minrow=0;
            int mincol=0;
            xlssheet.cells[2, intcols + 1] = "制";
            xlssheet.cells[3, intcols + 1] = "表";
            xlssheet.cells[4, intcols + 1] = ":";
            xlssheet.cells[7, intcols + 1] = "一";
            xlssheet.cells[8, intcols + 1] = "校";
            xlssheet.cells[9, intcols + 1] = ":";
            xlssheet.cells[12, intcols + 1] = "二";
            xlssheet.cells[13, intcols + 1] = "校";
            xlssheet.cells[14, intcols + 1] = ":";
            xlssheet.cells[17, intcols + 1] = "验";
            xlssheet.cells[18, intcols + 1] = "收";
            xlssheet.cells[19, intcols + 1] = ":";
            xlssheet.cells[1, 3] = "施测日期:" + dtps3.text;
            xlssheet.cells[1, 7] = "测探工具:" + txtctgongju.text;
            xlssheet.cells[1, 10] = "水位:" + txts4.text + "m";
            // xlssheet.cells[1, intcols] = "制表:";


            for (int j = 0; j < intcols; j++)
            {
                xlssheet.cells[2, j + 1] = strcolname[j];
            }

            for (int i = 0; i < introws; i++)
            {
                for (int j = 0; j < intcols; j++)
                {
                    if (i == temprows2 && j == tempcols2)
                    {
                    mydt1.rows[i][j]=mydt1.rows[i][j].tostring()+"▲";
                    }
                
                    if (i == temprows1 && j == tempcols1)
                    {
                        mydt1.rows[i][j] = mydt1.rows[i][j].tostring() + "▼";
                    }

                   //mydt1.rows[temprows3 + 3][tempcols3] = mydt1.rows[temprows3 + 3][tempcols3] + "       ";
                   // mydt1.rows[temprows3 + 3][tempcols3 + 1] = mydt1.rows[temprows3 + 3][tempcols3 + 1] + "       "; 

                 xlsapp.cells[i + 3, j + 1] = mydt1.rows[i][j];                  
                }
            }
            xlssheet.cells[introws + 3, 9] = "附注:" + txtfuzhu.text;
            xlssheet.columns.columnwidth = 9;
            xlssheet.get_range(xlssheet.cells[2, intcols + 1], xlssheet.cells[introws + 2, intcols + 1]).columnwidth = 3;

            xlssheet.rows.rowheight = 18;
            xlssheet.get_range(xlssheet.cells[2, 1], xlssheet.cells[2, intcols]).rowheight = 15;

            xlssheet.cells.font.name = "宋体";
            xlssheet.cells.font.size = 10;
            xlssheet.cells.font.bold = false;
            xlssheet.cells.wraptext = false;
            xlssheet.get_range(xlssheet.cells[2, 1], xlssheet.cells[2, intcols]).font.name = "宋体";
            xlssheet.get_range(xlssheet.cells[2, 1], xlssheet.cells[2, intcols]).font.size = 10;
            xlssheet.get_range(xlssheet.cells[2, 1], xlssheet.cells[2, intcols]).font.bold = true;
            xlssheet.get_range(xlssheet.cells[2, 1], xlssheet.cells[2, intcols]).wraptext = false;

            xlssheet.get_range(xlssheet.cells[temprows3 + 3, tempcols3 ], xlssheet.cells[temprows3 + 3, tempcols3+1]).font.underline = true;          


            for (int i = 0; i < mydt2.rows.count; i++)
            {
                for (int j = 0; j < mydt2.columns.count; j++)
                {
                    double temp1;
                    if (mydt2.rows[i][j].tostring() == "")
                    {
                        temp1 = 0.123456;
                    }
                    else
                    {
                        temp1 = convert.todouble(mydt2.rows[i][j].tostring());
                    }

                    if (j % 2 == 0)
                    {
                        if (temp1 == 0)
                        {
                            xlssheet.cells[i + 3, j + 1] = 0;
                        }
                        else if (temp1 == 0.123456)
                        {
                            xlssheet.cells[i + 3, j + 1] = "";
                        }
                        else
                        {
                            xlssheet.get_range(xlssheet.cells[i + 3, j + 1], xlssheet.cells[i + 3, j + 1]).numberformatlocal = "#####";
                        }
                    }
                    else
                    {
                        if (temp1 > 9)
                        {
                            xlssheet.get_range(xlssheet.cells[i + 3, j + 1], xlssheet.cells[i + 3, j + 1]).numberformatlocal = "#####.0";
                        }
                    }
                }
            }
            xlssheet.columns.verticalalignment = excel.xlvalign.xlvaligncenter;
            xlssheet.columns.horizontalalignment = excel.xlhalign.xlhalignright;
            xlssheet.get_range(xlssheet.cells[1, intcols], xlssheet.cells[1, intcols]).horizontalalignment = excel.xlhalign.xlhalignright;

            xlssheet.get_range(xlssheet.cells[2, 1], xlssheet.cells[introws + 2, intcols]).borders.linestyle = excel.xllinestyle.xlcontinuous;
            xlssheet.get_range(xlssheet.cells[2, 1], xlssheet.cells[introws + 2, intcols]).borders.weight = excel.xlborderweight.xlthin;

            //dgv.columns["高程6"].defaultcellstyle.format = "####0.0";
            xlssheet.pagesetup.centerheader = "&/"黑体/"&18" + lbldmmc.text + " 断 面 实 测 成 果 表";
            //xlssheet.pagesetup.centerfooter = "&9----- 第 &p 页 -----";          

            xlssheet.pagesetup.printgridlines = false;
            //xlssheet.get_range(xlssheet.cells[1,1],xlssheet.cells[1,intcols]).borders.linestyle
            xlssheet.pagesetup.leftmargin = xlsapp.inchestopoints(0.5);
            xlssheet.pagesetup.rightmargin = xlsapp.inchestopoints(0.5);
            xlssheet.pagesetup.topmargin = xlsapp.inchestopoints(1.3);
            xlssheet.pagesetup.bottommargin = xlsapp.inchestopoints(1.0);
            xlssheet.pagesetup.headermargin = xlsapp.inchestopoints(0.9);
            xlssheet.pagesetup.footermargin = xlsapp.inchestopoints(0.6);
            xlssheet.pagesetup.centerhorizontally = true;
            xlssheet.pagesetup.papersize = excel.xlpapersize.xlpapera4;
            xlssheet.pagesetup.orientation = excel.xlpageorientation.xllandscape;
            excel.range getrange = xlssheet.get_range(xlssheet.cells[1, 1], xlssheet.cells[2, intcols]);
            xlssheet.pagesetup.printtitlerows = getrange.get_address(getrange.row, getrange.column, excel.xlreferencestyle.xla1, 1, 1);


            xlsapp.visible = true;
            xlssheet.printpreview(true);
            xlsbook.saved = true;
            xlsapp.workbooks.close();
            xlsapp.visible = false;
            xlsapp.quit();
        } 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表