Exel 利用模板导出方法

    #region Exel导出方法

        [MaxuniAuthAttribute(Roles = "sysroles")]

        public void OrderExport(string id)

        {

            string strWhere = " from VOutProceExel where OProcessRequisitionId='" + id + "'";

            var obj = objBaseRepository.GetOneRecord(id);//数据库,表里的信息

            var objlist = objVOutProceExelRepository.GetAll(strWhere);//要导出的视图信息

            string path = Server.MapPath("~/exceltemplate/XX合同.xls");//模板

            FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read);//用模板导出







            string suppliercontactperson1 = "";

            string supplierName = "";

            string supplierpaymentType = "";

            string suppliertel = "";

            string supplierfax = "";

            string supplierOrderNo = "";

            GetSupplierinfo(id, out  suppliercontactperson1, out  supplierName, out  supplierpaymentType, out  suppliertel, out  supplierfax, out  supplierOrderNo);



            string CompayName = "";

            string address = "";

            string tel = "";

            string fax = "";

            string homepage = "";

            string email = "";

            GetCompanyAddres(out  CompayName, out  address, out  tel, out  fax, out  homepage, out  email);



            IWorkbook hssfworkbook = WorkbookFactory.Create(file);//创建Execl

            ICellStyle style = hssfworkbook.CreateCellStyle();

            ISheet sheetk3 = hssfworkbook.GetSheet("inquiry");//导出的Execl里的工作表名称,一定要一致



            sheetk3.DefaultColumnWidth = 200 * 20;

            sheetk3.DefaultRowHeightInPoints = 1000;

            sheetk3.DisplayGridlines = true;





            style.BorderBottom = BorderStyle.THIN;

            style.BorderLeft = BorderStyle.THIN;

            style.BorderRight = BorderStyle.THIN;

            style.BorderTop = BorderStyle.THIN;

            int count = objlist.Count;

            

            //

            IRow row1 = sheetk3.GetRow(2);//.GetRow(2)代表Execl模板里的行数,2代表第二行

            row1.GetCell(0).SetCellValue("甲方(委托方):XX有限公司");//.GetCell(0)代表Execl模板里的列数,0代表第一列

            row1.GetCell(5).SetCellValue(obj.contractNO);//.GetCell(5)代表Execl模板里的列数,5代表第五列



            IRow row2 = sheetk3.GetRow(3);//代表Execl模板里的行数,3代表第三行

            row2.CreateCell(0).SetCellValue("乙方(加工方):");

            row2.CreateCell(2).SetCellValue(obj.supplierName);//



            IRow row3 = sheetk3.GetRow(6);//.GetRow(6)代表Execl模板里的行数,6代表第6行

            row3.CreateCell(0).SetCellValue("一、内容:");





            for (int i = 0; i < count; i++)

            {

                IRow row = sheetk3.CreateRow(7 + i);//添加序列号,从第7行开始加,加到i行

                row.CreateCell(0).SetCellValue(i + 1);

                row.CreateCell(1).SetCellValue(objlist[i].mouldNo);

                row.CreateCell(2).SetCellValue(objlist[i].partName);

                row.CreateCell(3).SetCellValue(objlist[i].drawingNo);

                row.CreateCell(4).SetCellValue(objlist[i].OutTypeName);

                row.CreateCell(5).SetCellValue(objlist[i].qty);

                row.CreateCell(6).SetCellValue(objlist[i].processRemark);

                row.CreateCell(7).SetCellValue(objlist[i].Price);

                row.CreateCell(8).SetCellValue(objlist[i].TotaPricel);

                row.CreateCell(9).SetCellValue(objlist[i].oendDate == null ? "" : objlist[i].oendDate.Value.ToString("yyyy-MM-dd"));



                row.Cells[0].CellStyle = style;

                row.Cells[1].CellStyle = style;

                row.Cells[2].CellStyle = style;

                row.Cells[3].CellStyle = style;

                row.Cells[4].CellStyle = style;

                row.Cells[5].CellStyle = style;

                row.Cells[6].CellStyle = style;

                row.Cells[7].CellStyle = style;

                row.Cells[8].CellStyle = style;

                row.Cells[9].CellStyle = style;



            }



            IRow row4 = sheetk3.GetRow(29);

            string strdiscount = "";

            if (obj.discount == 100)

                strdiscount = "";

            else

                strdiscount = "折扣:" + obj.discount + "%";

            row4.GetCell(8).SetCellValue(obj.discountPrice.ToString());

            row4.GetCell(9).SetCellValue(strdiscount);



            IRow row5 = sheetk3.GetRow(49);

            row5.GetCell(1).SetCellValue("付款方式:" + supplierpaymentType);



            IRow row6 = sheetk3.GetRow(56);

            row6.GetCell(2).SetCellValue(obj.outUserName);







            MemoryStream ms = new MemoryStream();

            hssfworkbook.Write(ms);//

            Response.ContentType = "application/vnd.ms-excel";

            if (obj.contractNO == null)

            {

                Response.AddHeader("Content-Disposition", string.Format("attachment;filename=inquiry.xls"));

            }

            else

            {

                Response.AddHeader("Content-Disposition", string.Format("attachment;filename=" + obj.contractNO + ".xls"));

            }

            Response.BinaryWrite(ms.ToArray());

            Response.End();

            hssfworkbook = null;

            ms.Close();

            ms.Dispose();



        }

        #endregion



        #region 公司地址及详细信息

        private void GetCompanyAddres(out string CompayName, out string address, out string tel, out string fax, out string homepage, out string email)

        {

            var model = objCompayRepositoryRepository.GetAll();

            CompayName = "";

            address = "";

            tel = "";

            fax = "";

            homepage = "";

            email = "";

            if (model.Count > 0)

            {

                CompayName = model[0].CompayName;

                address = model[0].address;

                tel = model[0].tel;

                fax = model[0].fax;

                homepage = model[0].homepage;

                email = model[0].email;

                //ViewData["companyname"] = model[0].CompayName;

                //ViewData["address"] = model[0].address;

                //ViewData["tel"] = model[0].tel;

                //ViewData["fax"] = model[0].fax;

                //ViewData["homepage"] = model[0].homepage;

                //ViewData["email"] = model[0].email;

            }

        }

        #endregion



        #region 供应商地址级详细信息 合同条约





        private void GetSupplierinfo(string id, out string suppliercontactperson1, out string supplierName, out string supplierpaymentType, out string suppliertel, out string supplierfax, out string supplierOrderNo)

        {

            OProcessRequisition objOrderApply = objBaseRepository.GetOneRecord(id);

            string strSupplierid = objOrderApply.supplierID;

            Supplier objsupplier = new Supplier();

            if (!string.IsNullOrEmpty(strSupplierid))

                objsupplier = objSupplierRepository.GetOneRecord(strSupplierid);



            suppliercontactperson1 = objsupplier.contactperson1;

            supplierName = objsupplier.supplierName;

            supplierpaymentType = objsupplier.paymentType + ";税率:" + objsupplier.taxRate;

            suppliertel = objsupplier.tel;

            supplierfax = objsupplier.fax;

            supplierOrderNo = objOrderApply.contractNO;



            IList<OrderCond> objordercond = objOrderCondRepository.GetAll("from OrderCond where  TypeNo=1").OrderBy(o => o.SortId).ToList();

            //var objplaymenthod = objordercond.FirstOrDefault(o => o.SortId ==6);

            //var obj = objplaymenthod;

            //objordercond.Remove(objplaymenthod);

            //obj.CondContent = objplaymenthod.CondContent.Replace(":", "").Replace(":", "") + ":" + objsupplier.paymentType + ";税率:" + objsupplier.taxRate;

            //objordercond.Insert(11, obj);

            ViewData["allcond"] = objordercond;

        }

        #endregion

 

你可能感兴趣的:(exe)