Csharp: Create Excel Workbook or word from a Template File using aspose.Word 14.5 and aspose.Cell 8.1
winform:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnGenWord_Click( object sender, EventArgs e) { Dictionary< string , string > dictSource = new Dictionary< string , string >(); dictSource.Add( "NO" , "T0001" ); dictSource.Add( "INDUSTRY" , "捷为工作室" ); dictSource.Add( "NAME" , "塗聚文" ); string templateFile =( "Templates/Templates.doc" ); Aspose.Words.Document doc = new Aspose.Words.Document(templateFile); //使用文本方式替换 foreach ( string name in dictSource.Keys) { doc.Range.Replace(name, dictSource[name], true , true ); } #region 使用书签替换模式 Aspose.Words.Bookmark bookmark = doc.Range.Bookmarks[ "SEX" ]; if (bookmark != null ) { bookmark.Text = "男" ; } bookmark = doc.Range.Bookmarks[ "TEL" ]; if (bookmark != null ) { bookmark.Text = "13824350518*" ; } #endregion doc.Save( "testAdvice" +DateTime.Now.ToString( "yyyyMMddHHmmssfff" )+ ".docx" ,Aspose.Words.SaveFormat.Docx); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnGenExcel_Click( object sender, EventArgs e) { Dictionary< string , string > dictSource = new Dictionary< string , string >(); dictSource.Add( "NO" , "T0001" ); dictSource.Add( "INDUSTRY" , "捷为工作室" ); dictSource.Add( "NAME" , "塗聚文" ); string templateFile = ( "Templates/Templates.xls" ); WorkbookDesigner designer = new WorkbookDesigner(); //designer.Workbook.FileName=templateFile; Aspose.Cells.Workbook work = new Workbook(templateFile); designer.Workbook.Copy(work); Aspose.Cells.Worksheet worksheet = designer.Workbook.Worksheets[0]; worksheet.Name = "geovindu" ; //使用文本替换 foreach ( string name in dictSource.Keys) { worksheet.Replace(name, dictSource[name]); } //使用绑定数据方式替换 designer.SetDataSource( "SEX" , "男" ); designer.SetDataSource( "TEL" , "13824350518*" ); designer.Process(); designer.Workbook.Save( "testAdvice.xlsx" ,Aspose.Cells.SaveFormat.Xlsx); } |
webform:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | /// <summary> /// https://github.com/aspose-words/Aspose.Words-for-.NET /// https://asposewords.codeplex.com/ /// https://asposednn.codeplex.com/ /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnGenWord_Click( object sender, EventArgs e) { Dictionary< string , string > dictSource = new Dictionary< string , string >(); dictSource.Add( "NO" , "T0001" ); dictSource.Add( "INDUSTRY" , "捷為工作室" ); dictSource.Add( "NAME" , "涂聚文" ); string templateFile = Server.MapPath( "./Templates/Templates.doc" ); Aspose.Words.Document doc = new Aspose.Words.Document(templateFile); //veb: 14.5 //使用文本方式替换 foreach ( string name in dictSource.Keys) { doc.Range.Replace(name, dictSource[name], true , true ); } #region 使用书签替换模式 Aspose.Words.Bookmark bookmark = doc.Range.Bookmarks[ "SEX" ]; if (bookmark != null ) { bookmark.Text = "男" ; } //书签方式 bookmark = doc.Range.Bookmarks[ "TEL" ]; if (bookmark != null ) { bookmark.Text = "13824350518*" ; } #endregion string savefile = Server.MapPath( "./DuFile/geovindu.docx" ); doc.Save(savefile, Aspose.Words.SaveFormat.Docx); Response.Clear(); Response.Buffer = true ; //以字符流的形式下载文件 string fileName = "geovindu.docx" ; //下載文件名稱 FileStream fs = new FileStream(savefile, FileMode.Open); byte [] bytes = new byte [( int )fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); Response.ContentEncoding = System.Text.Encoding.UTF8; Response.HeaderEncoding = System.Text.Encoding.UTF8; Response.ContentType = "application/octet-stream" ; //通知浏览器下载文件而不是打开 Response.AddHeader( "Content-Disposition" , "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); //Response.AddHeader("Content-Length", fs.Length.ToString()); Response.BinaryWrite(bytes); Response.Flush(); Response.End(); } /// <summary> /// http://aspose.github.io/ /// https://github.com/asposemarketplace/Aspose_for_OpenXML /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button1_Click( object sender, EventArgs e) { Dictionary< string , string > dictSource = new Dictionary< string , string >(); dictSource.Add( "NO" , "T0002" ); dictSource.Add( "INDUSTRY" , "捷為工作室" ); dictSource.Add( "NAME" , "涂聚文" ); string templateFile = Server.MapPath( "./Templates/Templates.xls" ); WorkbookDesigner designer = new WorkbookDesigner(); //Veb:8.1 Aspose.Cells.Workbook work = new Workbook(templateFile); designer.Workbook.Copy(work); //designer.Open(templateFile); Aspose.Cells.Worksheet worksheet = designer.Workbook.Worksheets[0]; worksheet.Name = "geovindu" ; //使用文本替换 foreach ( string name in dictSource.Keys) { worksheet.Replace(name, dictSource[name]); } //使用绑定数据方式替换 designer.SetDataSource( "SEX" , "男" ); designer.SetDataSource( "TEL" , "13824350518*" ); designer.Process(); string savefile = Server.MapPath( "./DuFile/geovindu.xlsx" ); designer.Workbook.Save(savefile, Aspose.Cells.SaveFormat.Xlsx); string fileName = "geovindu.xlsx" ; //下載文件名稱 FileStream fs = new FileStream(savefile, FileMode.Open); byte [] bytes = new byte [( int )fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); Response.ContentEncoding = System.Text.Encoding.UTF8; Response.HeaderEncoding = System.Text.Encoding.UTF8; Response.ContentType = "application/octet-stream" ; //通知浏览器下载文件而不是打开 Response.AddHeader( "Content-Disposition" , "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); //Response.AddHeader("Content-Length", fs.Length.ToString()); Response.BinaryWrite(bytes); Response.Flush(); Response.End(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | /// <summary> /// 提取文檔中的圖片 /// http://doc.evget.com/HelpDocument/AsposeWordsforNETDocumentationCHM/Aspose.Words.Drawing.Shape.html /// </summary> public void ExtractImagesToFiles() { Document doc = new Document( "Image.SampleImages.doc" ); NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true ); int imageIndex = 0; foreach (Shape shape in shapes) { if (shape.HasImage) { string imageFileName = string .Format( "Image.ExportImages.{0} Out{1}" , imageIndex, FileFormatUtil.ImageTypeToExtension(shape.ImageData.ImageType)); shape.ImageData.Save(imageFileName); imageIndex++; } } // Newer Microsoft Word documents (such as DOCX) may contain a different type of image container called DrawingML. // Repeat the process to extract these if they are present in the loaded document. NodeCollection dmlShapes = doc.GetChildNodes(NodeType.DrawingML, true ); foreach (DrawingML dml in dmlShapes) { if (dml.HasImage) { string imageFileName = string .Format( "Image.ExportImages.{0} Out{1}" , imageIndex, FileFormatUtil.ImageTypeToExtension(dml.ImageData.ImageType)); dml.ImageData.Save(imageFileName); imageIndex++; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | //同页连续显示 Document dstDoc = new Document( "TestFile.Destination.doc" ); Document srcDoc = new Document( "TestFile.Source.doc" ); // Make the document appear straight after the destination documents content. srcDoc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous; // Append the source document using the original styles found in the source document. dstDoc.AppendDocument(srcDoc, ImportFormatMode.KeepSourceFormatting); dstDoc.Save( "TestFile.JoinContinuous Out.doc" ); //另起一页显示 // Set the appended document to start on a new page. srcDoc.FirstSection.PageSetup.SectionStart = SectionStart.NewPage; // Append the source document using the original styles found in the source document. dstDoc.AppendDocument(srcDoc, ImportFormatMode.KeepSourceFormatting); dstDoc.Save( "TestFile.JoinNewPage Out.doc" ); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | //实例化一个新的Word Document //也可以在Aspose.Words.Document doc = new Aspose.Words.Document(path)中加path参数, //此path指向你设计好的Word模板路径 Aspose.Words.Document doc = new Aspose.Words.Document(); DocumentBuilder builder = new DocumentBuilder(doc); //设置单元格内容对齐方式 builder.ParagraphFormat.Alignment = ParagraphAlignment.Left; //清除设置 builder.PageSetup.ClearFormatting(); DataTable tbl = new DataTable(); List< string > list = new List< string >(); if (tbl != null && tbl.Rows.Count > 0) { //加载小组 for ( int i = 0; i < tbl.Rows.Count; i++) { if (!list.Contains(tbl.Rows[i][ "XZ" ].ToString())) { list.Add(tbl.Rows[i][ "XZ" ].ToString()); } } double imgcellwidth = 85; double imgcellheight = 120; double cellwidth = 165; double cellheight = 18.5; //匹配小组中的学员 builder.StartTable(); //开始画Table builder.RowFormat.Alignment = Aspose.Words.Tables.RowAlignment.Center; string xz = string .Empty; int count = 0; int rowcount = 0; for ( int n = 0; n < list.Count; n++) { xz = list[n]; builder.RowFormat.Height = 20; //插入Table单元格 builder.InsertCell(); //Table单元格边框线样式 builder.CellFormat.Borders.LineStyle = LineStyle.Single; //Table此单元格宽度 builder.CellFormat.Width = 500; //此单元格中内容垂直对齐方式 builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center; builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.None; builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None; //字体大小 builder.Font.Size = 11; //是否加粗 builder.Bold = true ; //向此单元格中添加内容 builder.Write(xz); //Table行结束 builder.EndRow(); builder.Bold = false ; DataRow[] rows = tbl.Select( "xz='" + xz + "'" ); for ( int i = 0; i < rows.Length; i = i + 2) { count++; rowcount = (count - 1) * 6 + 1 + n; //第一行 builder.InsertCell(); builder.RowFormat.Height = imgcellheight; builder.CellFormat.Borders.LineStyle = LineStyle.Single; //合并行单元格 builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.First; builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center; builder.CellFormat.TopPadding = 2; builder.CellFormat.WrapText = false ; builder.CellFormat.Width = imgcellwidth; if (! string .IsNullOrEmpty(rows[i][ "ZPPATH" ].ToString())) { //向此单元格中插入图片 Shape shape = new Shape(doc, ShapeType.Image); string url = System.Configuration.ConfigurationManager.AppSettings[ "UserPhotosSitePath" ] + "\\" + rows[i][ "ZPPATH" ].ToString(); shape.ImageData.SetImage(url); shape.Width = imgcellwidth - 2; shape.Height = imgcellheight; shape.HorizontalAlignment = HorizontalAlignment.Center; CompositeNode node = shape.ParentNode; //把此图片移动到那个单元格中 builder.MoveToCell(0, rowcount, 0, 0); builder.InsertNode(shape); } builder.InsertCell(); builder.RowFormat.Height = cellheight; builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None; builder.CellFormat.Borders.LineStyle = LineStyle.Single; builder.CellFormat.Width = cellwidth; builder.Write(rows[i][ "XM" ].ToString()); builder.InsertCell(); builder.RowFormat.Height = imgcellheight; builder.CellFormat.Borders.LineStyle = LineStyle.Single; //合并行单元格 builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.First; builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center; builder.CellFormat.Width = imgcellwidth; if (rows.Length > i + 1) { if (! string .IsNullOrEmpty(rows[i + 1][ "ZPPATH" ].ToString())) { Shape shape = new Shape(doc, ShapeType.Image); string url = System.Configuration.ConfigurationManager.AppSettings[ "UserPhotosSitePath" ] + "\\" + rows[i + 1][ "ZPPATH" ].ToString(); shape.ImageData.SetImage(url); shape.Width = imgcellwidth - 2; shape.Height = imgcellheight; shape.HorizontalAlignment = HorizontalAlignment.Center; CompositeNode node = shape.ParentNode; builder.MoveToCell(0, rowcount, 2, 0); builder.InsertNode(shape); } } builder.InsertCell(); builder.RowFormat.Height = cellheight; builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None; builder.CellFormat.Borders.LineStyle = LineStyle.Single; builder.CellFormat.Width = cellwidth; if (rows.Length > i + 1) { builder.Write(rows[i + 1][ "XM" ].ToString()); } builder.EndRow(); //第二行 builder.InsertCell(); //此单元格与上一行单元格合并 builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.Previous; builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.None; builder.CellFormat.Width = imgcellwidth; builder.InsertCell(); builder.RowFormat.Height = cellheight; builder.CellFormat.Borders.LineStyle = LineStyle.Single; builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None; builder.CellFormat.Width = cellwidth; builder.Write( "单位及职务:" + rows[i][ "SZDW" ].ToString()); builder.InsertCell(); //此单元格与上一行单元格合并 builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.Previous; builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.None; builder.CellFormat.Width = imgcellwidth; builder.InsertCell(); builder.RowFormat.Height = cellheight; builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None; builder.CellFormat.Borders.LineStyle = LineStyle.Single; builder.CellFormat.Width = cellwidth; if (rows.Length > i + 1) { builder.Write( "单位及职务:" + rows[i + 1][ "SZDW" ].ToString()); } builder.EndRow(); //第三行 builder.InsertCell(); //此单元格与上一行单元格合并 builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.Previous; builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.None; builder.CellFormat.Width = imgcellwidth; builder.InsertCell(); builder.RowFormat.Height = cellheight; builder.CellFormat.Borders.LineStyle = LineStyle.Single; builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None; builder.CellFormat.Width = cellwidth; builder.Write( "单位电话:" + rows[i][ "DWDH" ].ToString()); builder.InsertCell(); //此单元格与上一行单元格合并 builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.Previous; builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.None; builder.CellFormat.Width = imgcellwidth; builder.InsertCell(); builder.RowFormat.Height = cellheight; builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None; builder.CellFormat.Borders.LineStyle = LineStyle.Single; builder.CellFormat.Width = cellwidth; if (rows.Length > i + 1) { builder.Write( "单位电话:" + rows[i + 1][ "DWDH" ].ToString()); } builder.EndRow(); //第四行 builder.InsertCell(); //此单元格与上一行单元格合并 builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.Previous; builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.None; builder.CellFormat.Width = imgcellwidth; builder.InsertCell(); builder.RowFormat.Height = cellheight; builder.CellFormat.Borders.LineStyle = LineStyle.Single; builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None; builder.CellFormat.Width = cellwidth; builder.Write( "手机:" + rows[i][ "SJHM" ].ToString()); builder.InsertCell(); //此单元格与上一行单元格合并 builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.Previous; builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.None; builder.CellFormat.Width = imgcellwidth; builder.InsertCell(); builder.RowFormat.Height = cellheight; builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None; builder.CellFormat.Borders.LineStyle = LineStyle.Single; builder.CellFormat.Width = cellwidth; if (rows.Length > i + 1) { builder.Write( "手机:" + rows[i + 1][ "SJHM" ].ToString()); } builder.EndRow(); //第五行 builder.InsertCell(); //此单元格与上一行单元格合并 builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.Previous; builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.None; builder.CellFormat.Width = imgcellwidth; builder.InsertCell(); builder.RowFormat.Height = cellheight; builder.CellFormat.Borders.LineStyle = LineStyle.Single; builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None; builder.CellFormat.Width = cellwidth; builder.Write( "邮编:" + rows[i][ "DWYB" ].ToString()); builder.InsertCell(); //此单元格与上一行单元格合并 builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.Previous; builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.None; builder.CellFormat.Width = imgcellwidth; builder.InsertCell(); builder.RowFormat.Height = cellheight; builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None; builder.CellFormat.Borders.LineStyle = LineStyle.Single; builder.CellFormat.Width = cellwidth; if (rows.Length > i + 1) { builder.Write( "邮编:" + rows[i + 1][ "DWYB" ].ToString()); } builder.EndRow(); //第六行 builder.InsertCell(); //此单元格与上一行单元格合并 builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.Previous; builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.None; builder.CellFormat.Width = imgcellwidth; builder.InsertCell(); builder.RowFormat.Height = cellheight; builder.CellFormat.Borders.LineStyle = LineStyle.Single; builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None; builder.CellFormat.Width = cellwidth; builder.Write( "Email:" + rows[i][ "DZYJ" ].ToString()); builder.InsertCell(); //此单元格与上一行单元格合并 builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.Previous; builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.None; builder.CellFormat.Width = imgcellwidth; builder.InsertCell(); builder.RowFormat.Height = cellheight; builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None; builder.CellFormat.Borders.LineStyle = LineStyle.Single; builder.CellFormat.Width = cellwidth; if (rows.Length > i + 1) { builder.Write( "Email:" + rows[i + 1][ "DZYJ" ].ToString()); } builder.EndRow(); } } builder.EndTable(); } string name = "学员通讯录.doc" ; //以下载Word的形式打开Wrod //如图所示: doc.Save(name, Aspose.Words.SaveFormat.Docx); |
https://github.com/asposemarketplace/Aspose_for_OpenXML
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2010-08-04 C# 公农历算法 ChineseLunisolarCalendar的一点思路