首页 > 学院 > 开发设计 > 正文

csharp: Aspose.Words create table

2019-11-17 02:45:15
字体:
来源:转载
供稿:网友

csharp: aspose.Words create table

 /// <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", "463588883@QQ.com");                tbl.Rows.Add(2, "", "涂年生","缔创智能工程技术有限公司;网络工程师", "82397502", "13824350518", "518003", "geovindu@qq.com");                tbl.Rows.Add(3, "", "涂聚文", "缔建智能建筑工程有限公司;UI设计师", "82397503", "13824350518", "518003", "463588883@qq.com");                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
上一篇:.NET基础之自定义泛型

下一篇:DBHelper

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表