欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#窗体间通讯处理的几种方法总结

程序员文章站 2024-02-15 20:35:05
最近做项目遇到导出excel的问题总结一下:看代码:复制代码 代码如下:///      &...

最近做项目遇到导出excel的问题总结一下:
看代码:

复制代码 代码如下:

/// <summary>
        /// 生成excel的方法
        /// </summary>
        /// <param name="ds">dataset</param>
        /// <param name="url">excel存在服务器的相对地址</param>
        /// <returns></returns>
        private bool exportexcel(dataset ds, string path)
        {
            //创建标题行
            hssfworkbook workbook = new hssfworkbook();
            hssfsheet sheet = workbook.createsheet("报名情况");
            hssfrow rowtitle = sheet.createrow(0);


            //设置列宽
            sheet.setcolumnwidth(0, 30 * 256);
            sheet.setcolumnwidth(1, 30 * 256);
            sheet.setcolumnwidth(2, 30 * 256);
            sheet.setcolumnwidth(3, 30 * 256);
            sheet.setcolumnwidth(4, 30 * 256);
            sheet.setcolumnwidth(5, 30 * 256);
            sheet.setcolumnwidth(6, 30 * 256);
            sheet.setcolumnwidth(7, 30 * 256);
            sheet.setcolumnwidth(8, 30 * 256);
            sheet.setcolumnwidth(9, 30 * 256);

 


            //创建列
            rowtitle.createcell(0,hssfcelltype.string).setcellvalue("姓名");
            rowtitle.createcell(1, hssfcelltype.string).setcellvalue("资质证书编号");
            rowtitle.createcell(2, hssfcelltype.string).setcellvalue("职业资格等级");
            rowtitle.createcell(3, hssfcelltype.string).setcellvalue("性别");
            rowtitle.createcell(4, hssfcelltype.string).setcellvalue("身份证号");
            rowtitle.createcell(5, hssfcelltype.string).setcellvalue("从业信息识别卡编号");
            rowtitle.createcell(6, hssfcelltype.string).setcellvalue("原机构名称");
            rowtitle.createcell(7, hssfcelltype.string).setcellvalue("原机构编号");
            rowtitle.createcell(8, hssfcelltype.string).setcellvalue("变更机构名称");
            rowtitle.createcell(9, hssfcelltype.string).setcellvalue("变更机构编号");


            //dataset是一个datatale的集合,如果只是填充了1张表,则此表的id为0
            datatable dt = ds.tables[0];
            int i = 1;
            foreach (datarow row in dt.rows)
            {
                hssfrow newrow = sheet.createrow(i);
                newrow.createcell(0,hssfcelltype.string).setcellvalue(convert.tostring(row["r_xm"]));
                newrow.createcell(1, hssfcelltype.string).setcellvalue(convert.tostring(row["r_newzzbh"]));
                string jibie=string.empty;
                if (row["r_jb"].tostring()=="1")
                {
                    jibie = "一级";
                }
                else if (row["r_jb"].tostring() == "2")
                {
                    jibie = "二级";
                }
                else if (row["r_jb"].tostring() == "3")
                {
                    jibie = "三级";
                }
                newrow.createcell(2,hssfcelltype.string).setcellvalue(jibie);
                newrow.createcell(3,hssfcelltype.string).setcellvalue(convert.tostring(row["r_xb"]));
                newrow.createcell(4,hssfcelltype.string).setcellvalue(convert.tostring(row["user_id"]));
                newrow.createcell(5,hssfcelltype.string).setcellvalue(convert.tostring(row["r_kh"]));
                newrow.createcell(6,hssfcelltype.string).setcellvalue(convert.tostring(row["yjgmc"]));
                newrow.createcell(7,hssfcelltype.string).setcellvalue(convert.tostring(row["yjgbh"]));
                newrow.createcell(8,hssfcelltype.string).setcellvalue(convert.tostring(row["bjgmc"]));
                newrow.createcell(9,hssfcelltype.string).setcellvalue(convert.tostring(row["bjgbh"]));
                i++;
            }
            try
            {
                using (stream stream = file.open(path, filemode.openorcreate, fileaccess.readwrite))
                {
                    workbook.write(stream);
                }
                return true;
            }
            catch (exception)
            {
                return false;
                throw;
            }

        }


导出方法:
复制代码 代码如下:

/// <summary>
        /// 导出excel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void linkexport_click(object sender, eventargs e)
        {
            dataset ds = new dataset();
            ds = aprymanager.export();
            random rd = new random();
            int rd1= rd.next(111111,999999);
            string path = this.server.mappath("~\\anxieexecl\\") + datetime.now.tostring("yyyymmddhhmmss")+ rd1.tostring() + ".xls";
            if (!directory.exists(this.server.mappath("~\\anxieexecl\\")))
            {
                directory.createdirectory(this.server.mappath("~\\anxieexecl\\"));
            }
            bool status = exportexcel(ds,path);
            string redirectpath = "~\\anxieexecl\\" + path.substring(path.lastindexof("\\") + 1);
            if (status)
            {
                response.redirect(redirectpath);
                file.delete(path);
            }
            else
            {
                clientscript.registerstartupscript(gettype(), "alert", "alert('生成excel失败!')", true);
            }
        }