aspose导出word

来源:互联网 发布:决战紫禁之巅 知乎 编辑:程序博客网 时间:2024/05/16 18:54

   protected void LinkButton1_Click(object sender, EventArgs e)
        {
            #region 单个标签替换值
            //string tmppath = Server.MapPath("~/template.doc");
            //Document doc = new Document(tmppath); //载入模板
            //if (doc.Range.Bookmarks["name"] != null)
            //{
            //    Bookmark mark = doc.Range.Bookmarks["name"];
            //    mark.Text = "张三公司";
            //}
            //doc.Save("demo.doc", SaveFormat.Doc, SaveType.OpenInWord, Response);
            #endregion
            /////////////////
            #region
            string tmppath = Server.MapPath("~/template.doc");
            Document doc = new Document(tmppath); //载入模板
            DocumentBuilder builder = new DocumentBuilder(doc);
            DocumentBuilder builder2 = new DocumentBuilder(doc);
            DataTable products = DBaseClass.db.GetSqlStrTable("select  username,userid from tsys_user"); ; //数据源

            int count = 0;
            //记录要显示多少列
            for (var i = 0; i < products.Columns.Count; i++)
            {
                if (doc.Range.Bookmarks[products.Columns[i].ColumnName.Trim()] != null)
                {
                    Bookmark mark = doc.Range.Bookmarks[products.Columns[i].ColumnName.Trim()];
                    mark.Text = "";
                    count++;
                }
            }
            System.Collections.Generic.List<string> listcolumn = new System.Collections.Generic.List<string>(count);
            for (var i = 0; i < count; i++)
            {
                builder.MoveToCell(0, 0, i, 0); //移动单元格
                if (builder.CurrentNode.NodeType == NodeType.BookmarkStart)
                {
                    listcolumn.Add((builder.CurrentNode as BookmarkStart).Name);
                }
            }
            double width = builder.CellFormat.Width;//获取单元格宽度
            builder.MoveToBookmark("table");        //开始添加值
            for (var m = 0; m < products.Rows.Count; m++)
            {
                for (var i = 0; i < listcolumn.Count; i++)
                {
                    builder.InsertCell();            // 添加一个单元格  
                    builder.CellFormat.Borders.LineWidth = 1;
                    builder.CellFormat.Borders.LineStyle = LineStyle.Single;
                    builder.CellFormat.Borders.Color = System.Drawing.Color.Black;
                    builder.CellFormat.Width = width;
                    builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None;
                    builder.Write(products.Rows[m][listcolumn[i]].ToString());
                }
                builder.EndRow();
            }


          

            doc.Range.Bookmarks["table"].Text = "";    // 清掉标示  
            doc.Save("baojiadan.doc", SaveFormat.Doc, SaveType.OpenInWord, Response);

            #endregion
        }

图片是模板的内容

【】是用的标签

【】用户名 【】 性别

【标签table】

 

合并单元格

builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.First;
builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.Previous;
builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None;

 

builder.InsertCell();            // 添加一个单元格                             
                builder.CellFormat.Borders.LineStyle = LineStyle.Single;
                builder.CellFormat.Borders.LineWidth = 1;
                builder.Font.Size = 12;
                builder.Font.Bold = true;
                builder.CellFormat.Borders.Color = System.Drawing.Color.Black;

                builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.First;
                builder.CellFormat.Width = wone;

                builder.Write("市区");

 


builder.InsertCell();            // 添加一个单元格                             
                builder.CellFormat.Borders.LineStyle = LineStyle.Single;
                builder.CellFormat.Borders.LineWidth = 1;
                builder.Font.Size = 12;
                builder.Font.Bold = false;
                builder.CellFormat.Borders.Color = System.Drawing.Color.Black;
                builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;//垂直居中对齐
                builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;//水平居中对齐
                builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.Previous;
                builder.CellFormat.Width = wone;
                builder.CellFormat.Shading.BackgroundPatternColor = othercolor;
                builder.Write("");

 

 

 

winform下保存

 SaveFileDialog s = new SaveFileDialog();
            s.DefaultExt = ".doc";
            s.FileName = "日报word";
            if (s.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            doc.Save(s.FileName, SaveFormat.Doc);