首页 > 开发 > 综合 > 正文

C#下面调用 Excel 另存代码

2024-07-21 02:25:50
字体:
来源:转载
供稿:网友

private void btnsaveasdata_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);
           double mingaocheng = 0;//get_min(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;
            int zhanyonghang = 3;
            xlssheet.cells[2, 7] = lbldmmc.text + "断面实测成果表";
            xlssheet.cells[zhanyonghang+2, intcols + 1] = "制";
            xlssheet.cells[zhanyonghang + 3, intcols + 1] = "表";
            xlssheet.cells[zhanyonghang + 4, intcols + 1] = ":";
            xlssheet.cells[zhanyonghang + 7, intcols + 1] = "一";
            xlssheet.cells[zhanyonghang + 8, intcols + 1] = "校";
            xlssheet.cells[zhanyonghang + 9, intcols + 1] = ":";
            xlssheet.cells[zhanyonghang + 12, intcols + 1] = "二";
            xlssheet.cells[zhanyonghang + 13, intcols + 1] = "校";
            xlssheet.cells[zhanyonghang + 14, intcols + 1] = ":";
            xlssheet.cells[zhanyonghang + 17, intcols + 1] = "验";
            xlssheet.cells[zhanyonghang + 18, intcols + 1] = "收";
            xlssheet.cells[zhanyonghang + 19, intcols + 1] = ":";
            xlssheet.cells[zhanyonghang + 1, 3] = "施测日期:" + dtps3.text;
            xlssheet.cells[zhanyonghang + 1, 7] = "测探工具:测探杆 ";
            xlssheet.cells[zhanyonghang + 1, 10] = "水位:" + txts4.text + "m";
            // xlssheet.cells[1, intcols] = "制表:";
            for (int j = 0; j < intcols; j++)
            {
                xlssheet.cells[zhanyonghang + 2, j + 1] = strcolname[j];
            }
            int tempi = 1;

            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() + "▼";
                    }
                    xlsapp.cells[zhanyonghang + i + 3, j + 1] = mydt1.rows[i][j];
                }
            }

            xlssheet.cells[zhanyonghang + introws + 3,6] = "附注:" + txtfuzhu.text;
            xlssheet.columns.columnwidth = 9;
            xlssheet.get_range(xlssheet.cells[zhanyonghang + 2, intcols + 1], xlssheet.cells[zhanyonghang + introws + 2, intcols + 1]).columnwidth = 3;

            xlssheet.rows.rowheight = 18;
            xlssheet.get_range(xlssheet.cells[zhanyonghang + 2, 1], xlssheet.cells[zhanyonghang + 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.size = 15;

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

            xlssheet.get_range(xlssheet.cells[zhanyonghang + temprows3 + 3, tempcols3], xlssheet.cells[zhanyonghang + 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[zhanyonghang + i + 3, j + 1] = 0;
                        }
                        else if (temp1 == 0.123456)
                        {
                            xlssheet.cells[i + 3, j + 1] = "";
                        }
                        else
                        {
                            xlssheet.get_range(xlssheet.cells[zhanyonghang + i + 3, j + 1], xlssheet.cells[zhanyonghang + i + 3, j + 1]).numberformatlocal = "#####";
                        }
                    }
                    else
                    {
                        if (temp1 > 9)
                        {
                            xlssheet.get_range(xlssheet.cells[zhanyonghang + i + 3, j + 1], xlssheet.cells[zhanyonghang + i + 3, j + 1]).numberformatlocal = "#####.0";
                        }
                    }
                }
            }
            // xlssheet.get_range(xlssheet.cells[3, 6], xlssheet.cells[3, 6]).numberformatlocal = "#####.0";
            //xlssheet.get_range(xlssheet.cells[3, 4], xlssheet.cells[introws + 2, 4]).numberformatlocal = "#####.0";
            //xlssheet.get_range(xlssheet.cells[3, 6], xlssheet.cells[introws + 2, 6]).numberformatlocal = "#####.0";
            //xlssheet.get_range(xlssheet.cells[3, 8], xlssheet.cells[introws + 2, 8]).numberformatlocal = "#####.0";
            //xlssheet.get_range(xlssheet.cells[3, 10], xlssheet.cells[introws + 2, 10]).numberformatlocal = "#####.0";
            //xlssheet.get_range(xlssheet.cells[3, 12], xlssheet.cells[introws + 2, 12]).numberformatlocal = "#####.0";

            xlssheet.columns.verticalalignment = excel.xlvalign.xlvaligncenter;
            xlssheet.columns.horizontalalignment = excel.xlhalign.xlhaligncenter;
            xlssheet.get_range(xlssheet.cells[zhanyonghang + 1, intcols], xlssheet.cells[zhanyonghang + 1, intcols]).horizontalalignment = excel.xlhalign.xlhalignright;

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

            string filename = xlsapp.getsaveasfilename("断面实测成果表 .xls", "excel文件 (*.xls), *.xls", 1, "断面实测成果表", "断面实测成果表").tostring().trim();
            if (filename != "false" && filename != "")
            {
                xlssheet.saveas(filename, excel.xlfileformat.xlexcel9795, "", "", false, false, false, 1, 1, true);
            }
            xlsbook.saved = true;
            xlsapp.workbooks.close();
            xlsapp.quit();
        } 

,欢迎访问网页设计爱好者web开发。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表