Apose 套打
给web添加一个dll引用:Apose.Words
下载链接:http://yunpan.cn/cA7v6uceM6KVw 提取码 11df
在Global.asax里面的Application_Start方法添加:
License lic = new License(); string licPath = HttpContext.Current.Server.MapPath("/Plugs/Aspose.Words.lic"); lic.SetLicense(licPath);
添加完成后,会报错,把里面的应用换成
using Aspose.Words; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Security; using System.Web.SessionState;
在Web下面添加一个文件夹Plugs,里面有两个文件:
http://yunpan.cn/cfQ6H5VJ7w8DC 提取码 ef95
设置一个按钮:
<input type="button" name="button" id="Report" value="批量打印" />
给按钮绑定事件:
$("#Report").click(function () { window.open("../../Apose/StudentInfo.aspx?studentType=" + $("#selStudentType").val() + "&schoolCode=" + $("#selSchool").val() + "&district=" + $("#selDistrict").val()); });
StudentInfo.aspx 页面就是导出的页面了,后台代码:
protected void Page_Load(object sender, EventArgs e) { string schoolCode = Request.QueryString["schoolCode"].ToString(); string studentType = Request.QueryString["studentType"].ToString(); string district = Request.QueryString["district"].ToString(); print(schoolCode, studentType, district); } DataTable getDs(string schoolCode, string studentType, string district) { string strWhere = " 1=1"; if (!string.IsNullOrEmpty(studentType)) { strWhere += " and TypeCode='" + studentType + "'"; Session["TypeCode"] = studentType; } if (!string.IsNullOrEmpty(schoolCode)) { strWhere += " and PrimarySchoolCode ='" + schoolCode + "'"; } if (!string.IsNullOrEmpty(district)) { strWhere += " and DistrictCode='" + district + "'"; } DataTable dt = new BLL.ObjMethod().GetList("View_CryStudent", strWhere); return dt; } protected void print(string schoolCode, string studentType, string district) { Document doc = new Document(); DocumentBuilder docBuilder = new DocumentBuilder(doc); Document src = null; var dt = getDs(schoolCode, studentType, district); int count = dt.Rows.Count; show.Text = "预备打印学生数量为:"+count; DataRow dr; for (int i = 0; i < dt.Rows.Count; i++) { dr = dt.Rows[i]; if (dr["TypeCode"].ToString() == "10000") { src = new Document(Server.MapPath("/files/1.doc")); } if (dr["TypeCode"].ToString() == "10001") { src = new Document(Server.MapPath("/files/10.doc")); } src.Range.Replace("<Ф1Ф>", dr["Name"].ToString(), false, false); src.Range.Replace("<Ф2Ф>", dr["SexCode"].ToString(), false, false); src.Range.Replace("<Ф3Ф>", dr["Birthday2"].ToString(), false, false); src.Range.Replace("<Ф4Ф>", dr["Nation"].ToString(), false, false); src.Range.Replace("<Ф5Ф>", dr["EduId"].ToString(), false, false); src.Range.Replace("<Ф6Ф>", dr["RollId"].ToString(), false, false); src.Range.Replace("<Ф7Ф>", dr["CardID"].ToString(), false, false); src.Range.Replace("<Ф8Ф>", dr["HuKouName"].ToString() + dr["HukouAddress"].ToString(), false, false); src.Range.Replace("<Ф9Ф>", dr["HuZhuName"].ToString(), false, false); src.Range.Replace("<Ф10Ф>", dr["HuZhuRelationName"].ToString(), false, false); src.Range.Replace("<Ф11Ф>", dr["IsAllFamilyIn"].ToString(), false, false); string relation = dr["IsAllFamilyIn"].ToString(); if (relation == "父户口同在") { src.Range.Replace("<Ф12Ф>", dr["Father"].ToString(), false, false); } else if (relation == "母户口同在") { src.Range.Replace("<Ф12Ф>", dr["Mother"].ToString(), false, false); } else if (relation == "父母户口同在") { src.Range.Replace("<Ф12Ф>", dr["Father"].ToString() + "," + dr["Mother"].ToString(), false, false); } else { src.Range.Replace("<Ф12Ф>","", false, false); } src.Range.Replace("<Ф13Ф>", dr["HouseQuXianAddressName"].ToString() + dr["HouseAddress"].ToString(), false, false); src.Range.Replace("<Ф14Ф>", dr["HouseOwnerName"].ToString(), false, false); src.Range.Replace("<Ф15Ф>", dr["HouseOwnerRelationName"].ToString(), false, false); src.Range.Replace("<Ф16Ф>", dr["HouseOwnerCardID"].ToString(), false, false); src.Range.Replace("<Ф17Ф>", dr["Tenantry"].ToString(), false, false); src.Range.Replace("<Ф18Ф>", dr["GuardianName1"].ToString(), false, false); src.Range.Replace("<Ф19Ф>", dr["GuardianRelationName1"].ToString(), false, false); src.Range.Replace("<Ф20Ф>", dr["GuardianZhengZhi1"].ToString(), false, false); src.Range.Replace("<Ф21Ф>", dr["GuardianEduCode1"].ToString(), false, false); src.Range.Replace("<Ф22Ф>", dr["GuardianCompany1"].ToString(), false, false); src.Range.Replace("<Ф23Ф>", dr["GuardianTelephone1"].ToString(), false, false); src.Range.Replace("<Ф24Ф>", dr["GuardianName2"].ToString(), false, false); src.Range.Replace("<Ф25Ф>", dr["GuardianRelationName2"].ToString(), false, false); src.Range.Replace("<Ф26Ф>", dr["GuardianZhengZhi2"].ToString(), false, false); src.Range.Replace("<Ф27Ф>", dr["GuardianEduCode2"].ToString(), false, false); src.Range.Replace("<Ф28Ф>", dr["GuardianCompany2"].ToString(), false, false); src.Range.Replace("<Ф29Ф>", dr["GuardianTelephone2"].ToString(), false, false); src.Range.Replace("<Ф30Ф>", dr["xh"].ToString(), false, false); src.Range.Replace("<Ф31Ф>", dr["Name"].ToString(), false, false); src.Range.Replace("<Ф32Ф>", dr["TiJiaoNumber"].ToString(), false, false); src.Range.Replace("<Ф33Ф>", dr["PrimarySchoolName"].ToString(), false, false); if (dr["TypeCode"].ToString() == "10001") { src.Range.Replace("<Ф34Ф>", dr["JieDuTypeName"].ToString(), false, false); } InsertDocument(doc.LastSection.Body.LastParagraph, src); docBuilder.MoveToDocumentEnd(); docBuilder.InsertBreak(BreakType.PageBreak); jindu.Text = "已经准备学生数量:"+(i+1); } // string path = "/files/" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".pdf"; string path = "/files/" + "啊啊啊" + ".pdf"; doc.Save(Server.MapPath(path), SaveFormat.Pdf); download(path); msg.InnerHtml = "<a href=\""+ path +"\">如果您没有正常打开文件,请点击这里</a>"; } /// <summary> /// Inserts content of the external document after the specified node. /// Section breaks and section formatting of the inserted document are ignored. /// </summary> /// <param name="insertAfterNode">Node in the destination document after which the content /// should be inserted. This node should be a block level node (paragraph or table).</param> /// <param name="srcDoc">The document to insert.</param> static void InsertDocument(Node insertAfterNode, Document srcDoc) { // Make sure that the node is either a paragraph or table. if ((!insertAfterNode.NodeType.Equals(NodeType.Paragraph)) & (!insertAfterNode.NodeType.Equals(NodeType.Table))) throw new ArgumentException("The destination node should be either a paragraph or table."); // We will be inserting into the parent of the destination paragraph. CompositeNode dstStory = insertAfterNode.ParentNode; // This object will be translating styles and lists during the import. NodeImporter importer = new NodeImporter(srcDoc, insertAfterNode.Document, ImportFormatMode.KeepSourceFormatting); // Loop through all sections in the source document. foreach (Section srcSection in srcDoc.Sections) { // Loop through all block level nodes (paragraphs and tables) in the body of the section. foreach (Node srcNode in srcSection.Body) { // Let's skip the node if it is a last empty paragraph in a section. if (srcNode.NodeType.Equals(NodeType.Paragraph)) { Paragraph para = (Paragraph)srcNode; if (para.IsEndOfSection && !para.HasChildNodes) continue; } // This creates a clone of the node, suitable for insertion into the destination document. Node newNode = importer.ImportNode(srcNode, true); // Insert new node after the reference node. dstStory.InsertAfter(newNode, insertAfterNode); insertAfterNode = newNode; } } } private void download(string filePath) { Response.Redirect(filePath); } //private void download(string filePath) //{ // #region default value // //查看方式,作为附件下载还是在浏览器中打开。默认为附件下载。浏览器中打开:viewType="inline"; // string viewType = "attachment"; // //下载完成是否删除,默认不删除。 // bool toDelete = false; // //内容类型,不同文件格式的内容类型不同。 // string contentType = "application/pdf"; // //文件保存时的默认名称。 // string fileName = "print.pdf"; // //要下载的文件 // //string filePath = "/files/student.docx"; // #endregion // //#region Reset value // //HttpRequest httpRequest = HttpContext.Current.Request; // //if (!string.IsNullOrEmpty(httpRequest["viewType"])) // // viewType = httpRequest["viewType"]; // //if (!string.IsNullOrEmpty(httpRequest["toDelete"])) // // toDelete = httpRequest["toDelete"].ToLower() == "true"; // //if (!string.IsNullOrEmpty(httpRequest["filePath"])) // // filePath = httpRequest["filePath"]; // //if (!string.IsNullOrEmpty(httpRequest["fileName"])) // // fileName = httpRequest["fileName"]; // //if (!string.IsNullOrEmpty(httpRequest["contentType"])) // // contentType = httpRequest["contentType"]; // //#endregion // filePath = HttpContext.Current.Server.MapPath(filePath); // Stream fileStream = null; // bool fileExsits = File.Exists(filePath); // if (fileExsits) // fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); // else // return; // HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache); // HttpContext.Current.Response.BufferOutput = false; // fileName = HttpUtility.UrlEncode(fileName, Encoding.UTF8).Replace('+', ' '); // HttpContext.Current.Response.Clear(); // HttpContext.Current.Response.AppendHeader("Content-Disposition", string.Format("{0};filename=\"{1}\"", viewType, fileName)); // HttpContext.Current.Response.AddHeader("Content-Length", fileStream.Length.ToString()); // HttpContext.Current.Response.ContentType = contentType; // try // { // long dataToRead = fileStream.Length; // byte[] buffer; // const int buffSize = 1024 * 16; // int length = 0; // while (dataToRead > 0) // { // if (HttpContext.Current.Response.IsClientConnected) // { // buffer = new Byte[buffSize]; // length = fileStream.Read(buffer, 0, buffSize); // HttpContext.Current.Response.OutputStream.Write(buffer, 0, length); // dataToRead = dataToRead - length; // } // else // { // dataToRead = -1; // } // } // } // finally // { // fileStream.Close(); // fileStream.Dispose(); // if (toDelete) // { // //new FileInfo(filePath).Attributes = FileAttributes.Normal; // File.Delete(filePath); // } // } // HttpContext.Current.Response.End(); //}
word截图: