博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ExportGrid Aspose.Cells.dll
阅读量:6332 次
发布时间:2019-06-22

本文共 9496 字,大约阅读时间需要 31 分钟。

using Aspose.Cells;using Aspose.Words;using System;using System.Collections;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace WebApplication16{    public partial class WebForm1 : System.Web.UI.Page    {        public void ExportWord()        {            string filePath = Server.MapPath("~/Template.doc");            string filePath1 = Server.MapPath("~/Template1.doc");            //预先生成数据            List
studentData = new List
(); for (int i = 0; i < 50; i++) { studentData.Add(new Student() { name = "学生" + i.ToString("D3"), schoolName = "某某中学", num = i, score = i }); } //加载word模板。 Aspose.Words.Document doc = null; try { doc = new Aspose.Words.Document(); } catch (Exception ex) { } Aspose.Words.DocumentBuilder docWriter = new Aspose.Words.DocumentBuilder(doc); docWriter.StartTable(); double[] colWidth = new double[] { 45, 60, 33, 55 }; string[] colName = new string[] { "编号", "姓名", "分数", "学校" }; int pageSize = 0; for (int i = 0, j = studentData.Count; i < j; i++) { if (i == 47) { } if (pageSize== 0) { //word页刚开始,一个表格的开始,要插入一个表头 docWriter.InsertBreak(Aspose.Words.BreakType.ParagraphBreak); //docWriter.StartTable(); AsposeCreateCell(docWriter, colWidth[0], colName[0]); AsposeCreateCell(docWriter, colWidth[1], colName[1]); AsposeCreateCell(docWriter, colWidth[2], colName[2]); AsposeCreateCell(docWriter, colWidth[3], colName[3]); docWriter.EndRow(); //docWriter.EndTable(); } //else if (pageSize == 30)//经过测算,每页word中可以放置30行 //{ // //结束第一个表格,插入分栏符号,并开始另一个表格 // docWriter.EndTable(); // docWriter.InsertBreak(Aspose.Words.BreakType.ColumnBreak); // docWriter.InsertBreak(Aspose.Words.BreakType.ParagraphBreak); // docWriter.StartTable(); // AsposeCreateCell(docWriter, colWidth[0], colName[0]); // AsposeCreateCell(docWriter, colWidth[1], colName[1]); // AsposeCreateCell(docWriter, colWidth[2], colName[2]); // AsposeCreateCell(docWriter, colWidth[3], colName[3]); // docWriter.EndRow(); //} //else if (pageSize == 60)//word分栏为2栏,那么一页word可以放60行数据 //{ // //一页word完毕,关闭表格,并绘制下一页的表头。 // docWriter.EndTable(); // docWriter.InsertBreak(Aspose.Words.BreakType.PageBreak); // docWriter.InsertBreak(Aspose.Words.BreakType.ParagraphBreak); // docWriter.StartTable(); // AsposeCreateCell(docWriter, colWidth[0], colName[0]); // AsposeCreateCell(docWriter, colWidth[1], colName[1]); // AsposeCreateCell(docWriter, colWidth[2], colName[2]); // AsposeCreateCell(docWriter, colWidth[3], colName[3]); // docWriter.EndRow(); // pageSize = 0; //} pageSize++; //创建内容 AsposeCreateCell(docWriter, colWidth[0], studentData[i].num.ToString()); AsposeCreateCell(docWriter, colWidth[1], studentData[i].name); AsposeCreateCell(docWriter, colWidth[2], studentData[i].score.ToString()); AsposeCreateCell(docWriter, colWidth[3], studentData[i].schoolName+"_"+i); docWriter.EndRow(); }//end for docWriter.EndTable(); //保存文件 doc.Save(filePath1, Aspose.Words.SaveFormat.Doc); } public void AsposeCreateCell(Aspose.Words.DocumentBuilder builder, double width, string text) { builder.InsertCell(); builder.CellFormat.Borders.LineStyle = Aspose.Words.LineStyle.Single; builder.CellFormat.Borders.Color = System.Drawing.Color.Black; builder.CellFormat.Width = width;//单元格的宽度 builder.CellFormat.LeftPadding = 3;//单元格的左内边距 builder.CellFormat.RightPadding = 3;//单元格的右内边距 builder.RowFormat.Height = 20;//行高 builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None; builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;//垂直居中对齐 builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Center;//水平居中对齐 builder.Write(text); } protected virtual void ExportGrid(string[] Columns, string title, System.Data.DataTable table, ArrayList ignorColList, bool showHeader) { Workbook workbook = new Workbook(); //工作簿 Worksheet sheet = workbook.Worksheets[0]; //工作表 sheet.AutoFitColumns(); Cells cells = sheet.Cells;//单元格 Aspose.Cells.Style styleTitle = workbook.Styles[workbook.Styles.Add()];//新增样式 styleTitle.HorizontalAlignment = TextAlignmentType.Center;//文字居中 styleTitle.Font.Name = "宋体";//文字字体 styleTitle.Font.Size = 14;//文字大小 styleTitle.Font.IsBold = false;//粗体 //样式2 Aspose.Cells.Style style2 = workbook.Styles[workbook.Styles.Add()];//新增样式 style2.HorizontalAlignment = TextAlignmentType.Center;//文字居中 style2.Font.Name = "宋体";//文字字体 style2.Font.Size = 10;//文字大小 style2.Font.IsBold = false;//粗体 style2.IsTextWrapped = true;//单元格内容自动换行 style2.Borders[Aspose.Cells.BorderType.LeftBorder].LineStyle = CellBorderType.Thin; style2.Borders[Aspose.Cells.BorderType.RightBorder].LineStyle = CellBorderType.Thin; style2.Borders[Aspose.Cells.BorderType.TopBorder].LineStyle = CellBorderType.Thin; style2.Borders[Aspose.Cells.BorderType.BottomBorder].LineStyle = CellBorderType.Thin; Aspose.Cells.Style style3 = workbook.Styles[workbook.Styles.Add()];//新增样式 style3.HorizontalAlignment = TextAlignmentType.Center;//文字居中 style3.Font.Name = "宋体";//文字字体 style3.Font.Size = 10;//文字大小 style3.Borders[Aspose.Cells.BorderType.LeftBorder].LineStyle = CellBorderType.Thin; style3.Borders[Aspose.Cells.BorderType.RightBorder].LineStyle = CellBorderType.Thin; style3.Borders[Aspose.Cells.BorderType.TopBorder].LineStyle = CellBorderType.Thin; style3.Borders[Aspose.Cells.BorderType.BottomBorder].LineStyle = CellBorderType.Thin; if (!string.IsNullOrEmpty(title)) { int i = Columns.Length; if (ignorColList != null) { i = i - ignorColList.Count; } cells.Merge(0, 0, 1, i);//合并单元格 cells[0, 0].PutValue(title);//填写内容 cells[0, 0].SetStyle(styleTitle); } int start = string.IsNullOrEmpty(title) ? 0 : 1; int j = 0; if (showHeader) { for (var i = 0; i < Columns.Length; i++) { var c = Columns[i]; if (ignorColList != null && ignorColList.Contains(c)) { continue; } cells[start, j].PutValue(c); cells[start, j].SetStyle(style2); j++; } } for (var q = 0; q < table.Rows.Count; q++) { j = 0; var row = table.Rows[q]; for (var i = 0; i < Columns.Length; i++) { var c = Columns[i]; if (ignorColList != null && ignorColList.Contains(c)) { continue; } cells[start + 1 + q, j].PutValue(row[c]); cells[start + 1 + q, j].SetStyle(style3); cells.SetRowHeight(start + 1 + q, 25); j++; } } workbook.Save(string.Format("report.xls"), Aspose.Cells.SaveType.OpenInExcel, Aspose.Cells.FileFormatType.Excel2003, Response); Response.Flush(); Response.End(); } protected void Page_Load(object sender, EventArgs e) { System.Data.DataTable table = new System.Data.DataTable(); table.Columns.Add("id2", typeof(string)); table.Columns.Add("name2", typeof(string)); for (int i = 0; i < 3; i++) { System.Data.DataRow dr = table.NewRow(); dr["id2"] = "id2_" + i; dr["name2"] = "name2_" + i; table.Rows.Add(dr); } string[] columns = { "id2", "name2" }; ExportWord(); //ExportGrid(columns, "柯相彬表格",table,null, true); Response.Write("ni mei de"); } }}

 

转载于:https://www.cnblogs.com/kexb/p/9371603.html

你可能感兴趣的文章
64. Minimum Path Sum
查看>>
Windows Live Writer 使用指南
查看>>
分析iOS Crash文件,使用命令符号化iOS Crash文件
查看>>
R学习笔记 第五篇:字符串操作
查看>>
在Mac OS下配置PHP开发环境
查看>>
(转)介绍下Nuget在传统Asp.net项目中的使用
查看>>
C# ArcEngine 实现点击要素高亮并弹出其属性
查看>>
初识GO语言——安装Go语言
查看>>
SDK命令行操作
查看>>
基于Bootstrap的DropDownList的JQuery组件的完善版
查看>>
EXTJS学习系列提高篇:第二十四篇(转载)作者殷良胜,ext2.2打造全新功能grid系列--阅增删改篇...
查看>>
Hadoop MapReduce编程 API入门系列之分区和合并(十四)
查看>>
判断二叉树是否平衡、是否完全二叉树、是否二叉排序树
查看>>
并查集的应用之求解无向图中的连接分量个数
查看>>
7个神奇的jQuery 3D插件
查看>>
在线浏览PDF之PDF.JS (附demo)
查看>>
波形捕捉:(3)"捕捉设备"性能
查看>>
AliOS Things lorawanapp应用介绍
查看>>
美国人的网站推广方式千奇百怪
查看>>
java web学习-1
查看>>