程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> csharp: Aspose.Words create table,csharpaspose.words

csharp: Aspose.Words create table,csharpaspose.words

編輯:C#入門知識

csharp: Aspose.Words create table,csharpaspose.words


 /// <summary>
        /// 20141118
        /// Geovin Du
        /// Aspose.Words創建表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                //實例化一個新的Word Document

                //也可以在Aspose.Words.Document doc = newAspose.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();// m_db.GetDataTable(sql, m_cn);
                tbl.Columns.Add("XZ", typeof(int));
                tbl.Columns.Add("ZPPATH", typeof(string));
                tbl.Columns.Add("XM", typeof(string));
                tbl.Columns.Add("SZDW", typeof(string));
                tbl.Columns.Add("DWDH", typeof(string));
                tbl.Columns.Add("SJHM", typeof(string));
                tbl.Columns.Add("DWYB", typeof(string));
                tbl.Columns.Add("DZYJ", typeof(string));

                tbl.Rows.Add(1, "", "geovindu", "締友計算機信息技術有限公司;軟件工程師", "82397501", "13824350518", "518003", "[email protected]");
                tbl.Rows.Add(2, "", "塗年生","締創智能工程技術有限公司;網絡工程師", "82397502", "13824350518", "518003", "[email protected]");
                tbl.Rows.Add(3, "", "塗聚文", "締建智能建築工程有限公司;UI設計師", "82397503", "13824350518", "518003", "[email protected]");


                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.ParagraphFormat.Alignment = ParagraphAlignment.Center; // 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
                Aspose.Words.Saving.HtmlSaveOptions options = new Aspose.Words.Saving.HtmlSaveOptions(SaveFormat.Html);
                Aspose.Words.Saving.DocSaveOptions docoptions = new Aspose.Words.Saving.DocSaveOptions(SaveFormat.Doc);
                //如圖所示:Aspose.Words導出帶圖片人員信息到Word中
                //doc.Save(name, Aspose.Words.SaveFormat.Doc, Aspose.Words.SaveType.OpenInWord, Response);
                doc.Save(Page.Response, name, ContentDisposition.Attachment, docoptions);//13.3.1

            }
            catch (Exception ex)
            {
                Response.Write(ex.Message.ToString());
            }

        }

參考:http://www.cnblogs.com/wuhuacong/archive/2013/02/05/2893191.html

 /// <summary>
        /// 20141118
        /// Geovin Du
        /// Aspose.Words模板操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                Dictionary<string, string> dictSource = new Dictionary<string, string>();
                dictSource.Add("TIS_HANDLE_NO", "T0001");
                dictSource.Add("ACCUSE_INDUSTRY", "出租車");
                dictSource.Add("ACCUSER_NAME", "張三");
                dictSource.Add("ACCUSER_COMPANY", "締友計算機信息技術有限公司");
                dictSource.Add("ACCUSER_ID", "8888888888888");
                dictSource.Add("COME_TEL", "82397507");
                dictSource.Add("ACCUSER_ADDRESS", "深圳市羅湖區");
                dictSource.Add("ACCUSER_ZIPCODE", "518003");
                dictSource.Add("TO_DEPARTMENT", "六福");
                dictSource.Add("TAXI_DEPT", "資訊科技部");
                dictSource.Add("CONTENT", "生活如水,沒有薪水發");
                dictSource.Add("MEMO", "好來好樣的");
                dictSource.Add("FOREMANID", "李四");
                dictSource.Add("HANDLE_TIME", "2014年11月18日下午6:00");
                string templateFile = Server.MapPath("Advice.doc");  //現有模板文件
                Aspose.Words.Document doc = new Aspose.Words.Document(templateFile);

                //使用文本方式替換
                foreach (string name in dictSource.Keys)
                {
                    //doc.Range.Replace(name, dictSource[name], true, true);

                    Aspose.Words.Bookmark bookmarkd = doc.Range.Bookmarks[name];//插入的書簽名
                    foreach (KeyValuePair<string, string> kvp in dictSource)
                    {
                        
                        if (bookmarkd != null)
                        {
                            if (name == kvp.Key)
                            {
                                bookmarkd.Text = kvp.Value;
                            }
                            
                        }
                    }

                   
                    bookmarkd = doc.Range.Bookmarks[name];

                }

                #region 使用書簽替換模式

                Aspose.Words.Bookmark bookmark = doc.Range.Bookmarks["ACCUSER_SEX"];
                if (bookmark != null)
                {
                    bookmark.Text = "男";
                }
                bookmark = doc.Range.Bookmarks["ACCUSER_TEL"];
                if (bookmark != null)
                {
                    bookmark.Text = "1862029207*";
                }

                #endregion
                Aspose.Words.Saving.DocSaveOptions docoptions = new Aspose.Words.Saving.DocSaveOptions(SaveFormat.Doc);
                doc.Save(Page.Response, "testAdvice.doc", Aspose.Words.ContentDisposition.Attachment,
                   docoptions);//Aspose.Words.Saving.SaveOptions.CreateSaveOptions(Aspose.Words.SaveFormat.Doc)
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message.ToString());
            }
        }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved