You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2054 lines
90 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Aspose.Words;
using System.Data;
using System.IO;
using System.Drawing.Printing;
using NetLibrary.ReportPrint;
using NetLibrary;
namespace NetLibrary
{
public class MicrosoftWord
{
#region 导出Work测试蛟川
public string Exportjc()
{
WordReport report = new WordReport();
WordTable tb = new WordTable();
report.ListTables.Add(tb);
tb.ListTexts.Add(new WordText("教师个人信息", 20, "Center"));
tb.ListTexts.Add(new WordText("", 20, "Center"));
tb.ListTexts.Add(new WordText("个人标识码:" + Space(8) + "所在学校:宁波市镇海蛟川书院", 10, "Center"));
List<double> ListColumnWidth = new List<double>();
ListColumnWidth.Add(80);
ListColumnWidth.Add(120);
ListColumnWidth.Add(80);
ListColumnWidth.Add(150);
WordCell cell = null;
//第1行
var Row = new List<WordCell>();
tb.Rows.Add(Row);
cell = new WordCell();
cell.Width = ListColumnWidth[0];
cell.Text = "姓名";
cell.Background = Color.AliceBlue;
cell.TextAlign = "Right";
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[1];
cell.Text = "符水波";
cell.TextAlign = "Left";
cell.ColumnSpan = 2;
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[2];
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[3];
cell.ImageWidth = 100;
cell.ImageHeight = 150;
cell.Text = AppDomain.CurrentDomain.BaseDirectory + "Photo/刘学左.jpg";
cell.ContentType = WordCellType.ImagePath;
//cell.Text = "测试合并";
cell.RowSpan = 7;
Row.Add(cell);
//第2行
Row = new List<WordCell>();
tb.Rows.Add(Row);
cell = new WordCell();
cell.Width = ListColumnWidth[0];
cell.Text = "曾用名";
cell.Background = Color.AliceBlue;
cell.TextAlign = "Right";
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[1];
cell.Text = "符水波";
cell.TextAlign = "Left";
cell.ColumnSpan = 2;
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[2];
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[3];
cell.Text = "";
Row.Add(cell);
//第3行
Row = new List<WordCell>();
tb.Rows.Add(Row);
cell = new WordCell();
cell.Width = ListColumnWidth[0];
cell.Text = "性别";
cell.Background = Color.AliceBlue;
cell.TextAlign = "Right";
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[1];
cell.Text = "符水波";
cell.TextAlign = "Left";
cell.ColumnSpan = 2;
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[2];
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[3];
cell.Text = "";
Row.Add(cell);
//第4行
Row = new List<WordCell>();
tb.Rows.Add(Row);
cell = new WordCell();
cell.Width = ListColumnWidth[0];
cell.Text = "教职工号";
cell.Background = Color.AliceBlue;
cell.TextAlign = "Right";
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[1];
cell.Text = "符水波";
cell.TextAlign = "Left";
cell.ColumnSpan = 2;
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[2];
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[3];
cell.Text = "";
Row.Add(cell);
//第5行
Row = new List<WordCell>();
tb.Rows.Add(Row);
cell = new WordCell();
cell.Width = ListColumnWidth[0];
cell.Text = "国籍/地区";
cell.Background = Color.AliceBlue;
cell.TextAlign = "Right";
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[1];
cell.Text = "符水波";
cell.TextAlign = "Left";
cell.ColumnSpan = 2;
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[2];
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[3];
cell.Text = "";
Row.Add(cell);
//第6行
Row = new List<WordCell>();
tb.Rows.Add(Row);
cell = new WordCell();
cell.Width = ListColumnWidth[0];
cell.Text = "身份证件类型";
cell.Background = Color.AliceBlue;
cell.TextAlign = "Right";
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[1];
cell.Text = "符水波";
cell.TextAlign = "Left";
cell.ColumnSpan = 2;
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[2];
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[3];
cell.Text = "";
Row.Add(cell);
//第7行
Row = new List<WordCell>();
tb.Rows.Add(Row);
cell = new WordCell();
cell.Width = ListColumnWidth[0];
cell.Text = "身份证件号";
cell.Background = Color.AliceBlue;
cell.TextAlign = "Right";
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[1];
cell.Text = "符水波";
cell.TextAlign = "Left";
cell.ColumnSpan = 2;
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[2];
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[3];
cell.Text = "";
Row.Add(cell);
//第8行
Row = new List<WordCell>();
tb.Rows.Add(Row);
cell = new WordCell();
cell.Width = ListColumnWidth[0];
cell.Text = "身份证件号";
cell.Background = Color.AliceBlue;
cell.TextAlign = "Right";
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[1];
cell.Text = "符水波";
cell.TextAlign = "Left";
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[2];
cell.Text = "符水波";
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[3];
cell.Text = "符水波";
Row.Add(cell);
tb = new WordTable();
report.ListTables.Add(tb);
tb.ListTexts.Add(new WordText("", 20, "Center"));
tb.ListTexts.Add(new WordText("教师个人信息2", 10, "Center"));
tb.ListTexts.Add(new WordText("", 20, "Center"));
//第1行
Row = new List<WordCell>();
tb.Rows.Add(Row);
cell = new WordCell();
cell.Width = ListColumnWidth[0];
cell.Text = "姓名";
cell.Background = Color.AliceBlue;
cell.TextAlign = "Right";
Row.Add(cell);
return Export(report);
}
#endregion
#region 导出Work测试
public string ExportTest()
{
List<WordReport> listReport = new List<WordReport>();
int PageCount = 2;
for (int k = 0; k < PageCount; k++)
{
WordReport report = new WordReport();
listReport.Add(report);
WordTable tb = new WordTable();
report.ListTables.Add(tb);
tb.ListTexts.Add(new WordText("新生报名登记表", 20, "Center"));
tb.ListTexts.Add(new WordText("", 20, "Center"));
//宽595*高848
List<double> ListColumnWidth = new List<double>();
ListColumnWidth.Add(75);
ListColumnWidth.Add(75);
ListColumnWidth.Add(75);
ListColumnWidth.Add(75);
ListColumnWidth.Add(75);
ListColumnWidth.Add(75);
ListColumnWidth.Add(120);
double RowHeight = 30;
WordCell cell = null;
//第1行
var Row = new List<WordCell>();
tb.Rows.Add(Row);
cell = new WordCell();
cell.Width = ListColumnWidth[0];
cell.Height = RowHeight;
cell.Text = "姓名";
cell.Background = Color.AliceBlue;
cell.TextAlign = "Right";
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[1];
cell.Height = RowHeight;
cell.Text = "符水波";
cell.TextAlign = "Left";
cell.ColumnSpan = 3;
Row.Add(cell);
for (int i = 2; i < 4; i++)
{
cell = new WordCell();
cell.Width = ListColumnWidth[i];
cell.Height = RowHeight;
Row.Add(cell);
}
cell = new WordCell();
cell.Width = ListColumnWidth[4];
cell.Height = RowHeight;
cell.Text = "性别";
cell.Background = Color.AliceBlue;
cell.TextAlign = "Right";
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[5];
cell.Height = RowHeight;
cell.Text = "男";
cell.TextAlign = "Left";
cell.ColumnSpan = 2;
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[6];
cell.Height = RowHeight;
Row.Add(cell);
//第2行
Row = new List<WordCell>();
tb.Rows.Add(Row);
cell = new WordCell();
cell.Width = ListColumnWidth[0];
cell.Height = RowHeight;
cell.Text = "出生日期";
cell.Background = Color.AliceBlue;
cell.TextAlign = "Right";
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[1];
cell.Height = RowHeight;
cell.Text = "1996-01-01";
cell.TextAlign = "Left";
cell.ColumnSpan = 3;
Row.Add(cell);
for (int i = 2; i < 4; i++)
{
cell = new WordCell();
cell.Width = ListColumnWidth[i];
cell.Height = RowHeight;
Row.Add(cell);
}
cell = new WordCell();
cell.Width = ListColumnWidth[4];
cell.Height = RowHeight;
cell.Text = "身份证号码";
cell.Background = Color.AliceBlue;
cell.TextAlign = "Right";
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[5];
cell.Height = RowHeight;
cell.Text = "3302241978561415";
cell.TextAlign = "Left";
cell.ColumnSpan = 2;
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[6];
cell.Height = RowHeight;
Row.Add(cell);
//第3行
Row = new List<WordCell>();
tb.Rows.Add(Row);
cell = new WordCell();
cell.Width = ListColumnWidth[0];
cell.Height = RowHeight;
cell.Text = "户籍类别";
cell.Background = Color.AliceBlue;
cell.TextAlign = "Right";
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[1];
cell.Height = RowHeight;
cell.Text = "居民";
cell.TextAlign = "Left";
cell.ColumnSpan = 3;
Row.Add(cell);
for (int i = 2; i < 4; i++)
{
cell = new WordCell();
cell.Width = ListColumnWidth[i];
cell.Height = RowHeight;
Row.Add(cell);
}
cell = new WordCell();
cell.Width = ListColumnWidth[4];
cell.Height = RowHeight;
cell.Text = "就读幼儿园";
cell.Background = Color.AliceBlue;
cell.TextAlign = "Right";
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[5];
cell.Height = RowHeight;
cell.Text = "江东";
cell.TextAlign = "Left";
cell.ColumnSpan = 2;
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[6];
cell.Height = RowHeight;
Row.Add(cell);
//第4行
Row = new List<WordCell>();
tb.Rows.Add(Row);
cell = new WordCell();
cell.Width = ListColumnWidth[0];
cell.Height = RowHeight;
cell.Text = "户籍所在地";
cell.Background = Color.AliceBlue;
cell.TextAlign = "Right";
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[1];
cell.Height = RowHeight;
cell.Text = "浙江省宁波市江东区XXX街道1号106室";
cell.TextAlign = "Left";
cell.ColumnSpan = 6;
Row.Add(cell);
for (int i = 2; i < 7; i++)
{
cell = new WordCell();
cell.Width = ListColumnWidth[i];
cell.Height = RowHeight;
Row.Add(cell);
}
//第5行
Row = new List<WordCell>();
tb.Rows.Add(Row);
cell = new WordCell();
cell.Width = ListColumnWidth[0];
cell.Height = RowHeight;
cell.Text = "常住地址";
cell.Background = Color.AliceBlue;
cell.TextAlign = "Right";
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[1];
cell.Height = RowHeight;
cell.Text = "浙江省宁波市江东区XXX街道1号106室";
cell.TextAlign = "Left";
cell.ColumnSpan = 6;
Row.Add(cell);
for (int i = 2; i < 7; i++)
{
cell = new WordCell();
cell.Width = ListColumnWidth[i];
cell.Height = RowHeight;
Row.Add(cell);
}
//第6行
Row = new List<WordCell>();
tb.Rows.Add(Row);
cell = new WordCell();
cell.Width = ListColumnWidth[0];
cell.Height = RowHeight;
cell.Text = "电话";
cell.Background = Color.AliceBlue;
cell.TextAlign = "Right";
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[1];
cell.Height = RowHeight;
cell.Text = "6589952535";
cell.TextAlign = "Left";
cell.ColumnSpan = 6;
Row.Add(cell);
for (int i = 2; i < 7; i++)
{
cell = new WordCell();
cell.Width = ListColumnWidth[i];
cell.Height = RowHeight;
Row.Add(cell);
}
//第7行
Row = new List<WordCell>();
tb.Rows.Add(Row);
RowHeight = 200;
cell = new WordCell();
cell.Width = ListColumnWidth[0];
cell.Height = RowHeight;
cell.Text = "特长及荣誉";
cell.Background = Color.AliceBlue;
cell.TextAlign = "Right";
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[1];
cell.Height = RowHeight;
cell.Text = "";
cell.TextAlign = "Left";
cell.ColumnSpan = 6;
Row.Add(cell);
for (int i = 2; i < 7; i++)
{
cell = new WordCell();
cell.Width = ListColumnWidth[i];
cell.Height = RowHeight;
Row.Add(cell);
}
//第8行
Row = new List<WordCell>();
tb.Rows.Add(Row);
cell = new WordCell();
cell.Width = ListColumnWidth[0];
cell.Height = RowHeight;
cell.Text = "选择学校的原因";
cell.Background = Color.AliceBlue;
cell.TextAlign = "Right";
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[1];
cell.Height = RowHeight;
cell.Text = "";
cell.TextAlign = "Left";
cell.ColumnSpan = 6;
Row.Add(cell);
for (int i = 2; i < 7; i++)
{
cell = new WordCell();
cell.Width = ListColumnWidth[i];
cell.Height = RowHeight;
Row.Add(cell);
}
//第9行
Row = new List<WordCell>();
tb.Rows.Add(Row);
RowHeight = 30;
cell = new WordCell();
cell.Width = ListColumnWidth[0];
cell.Height = RowHeight;
cell.Text = "称谓";
cell.Background = Color.AliceBlue;
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[1];
cell.Height = RowHeight;
cell.Text = "姓名";
cell.Background = Color.AliceBlue;
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[2];
cell.Height = RowHeight;
cell.Text = "工作单位";
cell.Background = Color.AliceBlue;
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[3];
cell.Height = RowHeight;
cell.Text = "职务";
cell.Background = Color.AliceBlue;
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[4];
cell.Height = RowHeight;
cell.Text = "学历";
cell.Background = Color.AliceBlue;
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[5];
cell.Height = RowHeight;
cell.Text = "联系电话";
cell.Background = Color.AliceBlue;
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[6];
cell.Height = RowHeight;
cell.Text = "身份证号码";
cell.Background = Color.AliceBlue;
Row.Add(cell);
//第10行
Row = new List<WordCell>();
tb.Rows.Add(Row);
cell = new WordCell();
cell.Width = ListColumnWidth[0];
cell.Height = RowHeight;
cell.Text = "";
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[1];
cell.Height = RowHeight;
cell.Text = "";
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[2];
cell.Height = RowHeight;
cell.Text = "";
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[3];
cell.Height = RowHeight;
cell.Text = "";
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[4];
cell.Height = RowHeight;
cell.Text = "";
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[5];
cell.Height = RowHeight;
cell.Text = "";
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[6];
cell.Height = RowHeight;
cell.Text = "";
Row.Add(cell);
//第11行
Row = new List<WordCell>();
tb.Rows.Add(Row);
cell = new WordCell();
cell.Width = ListColumnWidth[0];
cell.Height = RowHeight;
cell.Text = "";
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[1];
cell.Height = RowHeight;
cell.Text = "";
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[2];
cell.Height = RowHeight;
cell.Text = "";
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[3];
cell.Height = RowHeight;
cell.Text = "";
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[4];
cell.Height = RowHeight;
cell.Text = "";
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[5];
cell.Height = RowHeight;
cell.Text = "";
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[6];
cell.Height = RowHeight;
cell.Text = "";
Row.Add(cell);
//第12行
Row = new List<WordCell>();
tb.Rows.Add(Row);
cell = new WordCell();
cell.Width = ListColumnWidth[0];
cell.Height = RowHeight;
cell.Text = "是否住宿";
cell.Background = Color.AliceBlue;
Row.Add(cell);
cell = new WordCell();
cell.Width = ListColumnWidth[1];
cell.Height = RowHeight;
cell.Text = "是";
cell.ColumnSpan = 6;
Row.Add(cell);
for (int i = 2; i < 7; i++)
{
cell = new WordCell();
cell.Width = ListColumnWidth[i];
cell.Height = RowHeight;
Row.Add(cell);
}
}
return Export(listReport);
}
#endregion
#region 导出Word
public string Export(WordReport report)
{
Aspose.Words.Document doc = new Aspose.Words.Document();
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder();
PageSetup ps = builder.PageSetup;
ps.LeftMargin = report.Left;//设置或修改边距
ps.TopMargin = report.Top;//设置或修改边距
ps.RightMargin = report.Right;//设置或修改边距
ps.BottomMargin = report.Bottom;//设置或修改边距
//ps.PaperSize = (PaperSize)report.PageType; //纸张类型
ps.Orientation = (Orientation)report.OrientationType; //纸张方向
builder.Document = doc;
foreach (var item in report.ListTables)
{
foreach (var item2 in item.ListTexts)
{
builder.Font.Name = item2.FontName;
builder.Font.Size = item2.FontSize;
builder.Font.Bold = item2.FontBold;
builder.Font.Color = item2.FontColor;
//builder.ParagraphFormat.Shading.BackgroundPatternColor= item2.Background;
if (item2.TextAlign == "Center") builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Center;//水平居中对齐
if (item2.TextAlign == "Right") builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Right;//水平居右对齐
if (item2.TextAlign == "Left") builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Left;//水平居右对齐
builder.Writeln(item2.Text);
}
item.MathMerge(); //计算合并单元格
Aspose.Words.Tables.Table table = builder.StartTable();
foreach (var row in item.Rows)
{
foreach (var cell in row)
{
builder.InsertCell();
builder.CellFormat.Borders.LineStyle = Aspose.Words.LineStyle.Single;
builder.CellFormat.Borders.Color = System.Drawing.Color.Black;
builder.CellFormat.HorizontalMerge = (Aspose.Words.Tables.CellMerge)cell.HorizontalCellMerge;
builder.CellFormat.VerticalMerge = (Aspose.Words.Tables.CellMerge)cell.VerticalCellMerge;
//单元格背景色
builder.CellFormat.Shading.BackgroundPatternColor = cell.Background;
if (cell.VerticalAlign == "Top") builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Top;//垂直居中对齐
if (cell.VerticalAlign == "Center") builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;//垂直居中对齐
if (cell.VerticalAlign == "Bottom") builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Bottom;//垂直居中对齐
//builder.Font.Shading.BackgroundPatternColor = Color.Red;
if (cell.TextAlign == "Center")
{
builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Center;//水平居中对齐
//builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;//垂直居中对齐
}
if (cell.TextAlign == "Right") builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Right;
if (cell.TextAlign == "Left") builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Left;
builder.CellFormat.Width = cell.Width;
if (cell.Height > 0) builder.RowFormat.Height = cell.Height;
builder.Font.Name = cell.FontName;
builder.Font.Size = cell.FontSize;
builder.Font.Color = cell.FontColor;
builder.Font.Bold = cell.FontBold;
if (cell.Text == null) cell.Text = "";
switch (cell.ContentType)
{
case WordCellType.None:
builder.Write(cell.Text);
break;
case WordCellType.ImagePath:
if (System.IO.File.Exists(cell.Text) == true)
{
builder.InsertImage(cell.Text, cell.ImageWidth, cell.ImageHeight);
}
else { builder.Write(""); }
break;
case WordCellType.ImageByte:
builder.InsertImage(cell.ImageBuffer, cell.ImageWidth, cell.ImageHeight);
break;
case WordCellType.Image:
builder.InsertImage(cell.ImagePhoto, cell.ImageWidth, cell.ImageHeight);
break;
default:
break;
}
}
builder.EndRow();
}
builder.EndTable();
foreach (var item2 in item.ListFooterTexts)
{
builder.Font.Name = item2.FontName;
builder.Font.Size = item2.FontSize;
builder.Font.Bold = item2.FontBold;
builder.Font.Color = item2.FontColor;
//builder.ParagraphFormat.Shading.BackgroundPatternColor= item2.Background;
if (item2.TextAlign == "Center") builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Center;//水平居中对齐
if (item2.TextAlign == "Right") builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Right;//水平居右对齐
if (item2.TextAlign == "Left") builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Left;//水平居右对齐
builder.Writeln(item2.Text);
}
//table.AutoFit(Aspose.Words.Tables.AutoFitBehavior.FixedColumnWidths);
}
string dir = AppDomain.CurrentDomain.BaseDirectory + "ServerCookies";
if (Directory.Exists(dir) == false) Directory.CreateDirectory(dir);
string fileName = "ServerCookies/" + Guid.NewGuid().ToString() + ".doc";
string TempFilePath = AppDomain.CurrentDomain.BaseDirectory + fileName;
doc.Save(TempFilePath);
return fileName;
}
#endregion
#region 导出Word_多页
public string Export(List<WordReport> ListReport)
{
Aspose.Words.Document dstdoc = new Aspose.Words.Document();
Aspose.Words.Document doc = null;
foreach (WordReport report in ListReport)
{
int index = ListReport.IndexOf(report);
if (index == 0)
{
doc = dstdoc;
}
else
{
doc = new Document();
doc.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;
}
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder();
//PageSetup ps = builder.PageSetup;
//PageSetup ps = doc.Sections[0].PageSetup;
//ps.LeftMargin = report.Left;//设置或修改边距
//ps.TopMargin = report.Top;//设置或修改边距
//ps.RightMargin = report.Right;//设置或修改边距
//ps.BottomMargin = report.Bottom;//设置或修改边距
//ps.PaperSize = (PaperSize)report.PageType; //纸张类型
//ps.Orientation = (Orientation)report.OrientationType; //纸张方向
builder.Document = doc;
foreach (var item in report.ListTables)
{
foreach (var item2 in item.ListTexts)
{
builder.Font.Name = item2.FontName;
builder.Font.Size = item2.FontSize;
builder.Font.Bold = item2.FontBold;
builder.Font.Color = item2.FontColor;
//builder.ParagraphFormat.Shading.BackgroundPatternColor= item2.Background;
if (item2.TextAlign == "Center") builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Center;//水平居中对齐
if (item2.TextAlign == "Right") builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Right;//水平居右对齐
if (item2.TextAlign == "Left") builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Left;//水平居右对齐
builder.Writeln(item2.Text);
}
item.MathMerge(); //计算合并单元格
Aspose.Words.Tables.Table table = builder.StartTable();
foreach (var row in item.Rows)
{
foreach (var cell in row)
{
builder.InsertCell();
builder.CellFormat.Borders.LineStyle = Aspose.Words.LineStyle.Single;
builder.CellFormat.Borders.Color = System.Drawing.Color.Black;
builder.CellFormat.HorizontalMerge = (Aspose.Words.Tables.CellMerge)cell.HorizontalCellMerge;
builder.CellFormat.VerticalMerge = (Aspose.Words.Tables.CellMerge)cell.VerticalCellMerge;
//单元格背景色
builder.CellFormat.Shading.BackgroundPatternColor = cell.Background;
if (cell.VerticalAlign == "Top") builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Top;//垂直居中对齐
if (cell.VerticalAlign == "Center") builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;//垂直居中对齐
if (cell.VerticalAlign == "Bottom") builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Bottom;//垂直居中对齐
//builder.Font.Shading.BackgroundPatternColor = Color.Red;
if (cell.TextAlign == "Center")
{
builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Center;//水平居中对齐
//builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;//垂直居中对齐
}
if (cell.TextAlign == "Right") builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Right;
if (cell.TextAlign == "Left") builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Left;
builder.CellFormat.Width = cell.Width;
if (cell.Height > 0) builder.RowFormat.Height = cell.Height;
builder.Font.Name = cell.FontName;
builder.Font.Size = cell.FontSize;
builder.Font.Color = cell.FontColor;
builder.Font.Bold = cell.FontBold;
if (cell.Text == null) cell.Text = "";
switch (cell.ContentType)
{
case WordCellType.None:
builder.Write(cell.Text);
break;
case WordCellType.ImagePath:
if (System.IO.File.Exists(cell.Text) == true)
{
builder.InsertImage(cell.Text, cell.ImageWidth, cell.ImageHeight);
}
else { builder.Write(""); }
break;
case WordCellType.ImageByte:
builder.InsertImage(cell.ImageBuffer, cell.ImageWidth, cell.ImageHeight);
break;
case WordCellType.Image:
builder.InsertImage(cell.ImagePhoto, cell.ImageWidth, cell.ImageHeight);
break;
default:
break;
}
}
builder.EndRow();
}
builder.EndTable();
foreach (var item2 in item.ListFooterTexts)
{
builder.Font.Name = item2.FontName;
builder.Font.Size = item2.FontSize;
builder.Font.Bold = item2.FontBold;
builder.Font.Color = item2.FontColor;
//builder.ParagraphFormat.Shading.BackgroundPatternColor= item2.Background;
if (item2.TextAlign == "Center") builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Center;//水平居中对齐
if (item2.TextAlign == "Right") builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Right;//水平居右对齐
if (item2.TextAlign == "Left") builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Left;//水平居右对齐
builder.Writeln(item2.Text);
}
}
if (index > 0) dstdoc.AppendDocument(doc, ImportFormatMode.KeepSourceFormatting);
}
string dir = AppDomain.CurrentDomain.BaseDirectory + "ServerCookies";
if (Directory.Exists(dir) == false) Directory.CreateDirectory(dir);
string fileName = "ServerCookies/" + Guid.NewGuid().ToString() + ".doc";
string TempFilePath = AppDomain.CurrentDomain.BaseDirectory + fileName;
dstdoc.Save(TempFilePath);
return fileName;
}
#endregion
#region 导出Word模板替换
public string Export(string WordTemplatePath, DataTable source, TableColumnCollection listColumns)
{
if (WordTemplatePath.IndexOf(":") == -1) WordTemplatePath = AppDomain.CurrentDomain.BaseDirectory + WordTemplatePath;
Aspose.Words.Document dstdoc = new Aspose.Words.Document(WordTemplatePath);
Aspose.Words.Document doc = null;
foreach (DataRow row in source.Rows)
{
int index = source.Rows.IndexOf(row);
if (index == 0)
{
doc = dstdoc;
}
else
{
doc = new Aspose.Words.Document(WordTemplatePath);
doc.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;
}
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder();
builder.Document = doc;
foreach (Bookmark item in doc.Range.Bookmarks)
{
var col = listColumns.GetTableColumn(item.Name);
if (col == null) continue;
if (col.KeyValueFormat.Count > 0)
{
string v = Convert.ToString(row[item.Name]);
foreach (var Keyitem in col.KeyValueFormat)
{
if (Keyitem.Key == v) item.Text = Keyitem.Value;
}
}
else if (col.DataType == DbType.Object)
{
string[] ss = col.Format.Split('*');
double width = Convert.ToDouble(ss[0]);
double height = Convert.ToDouble(ss[1]);
string filePath = Convert.ToString(row[item.Name]);
if (filePath.IndexOf(":") == -1) filePath = AppDomain.CurrentDomain.BaseDirectory + filePath;
//ErrorFollow.TraceWrite("导出word照片", "", filePath);
if (System.IO.File.Exists(filePath) == true)
{
builder.MoveToBookmark(item.Name);
builder.InsertImage(filePath, width, height);
}
}
else if (col.DataType == DbType.Binary)
{
string[] ss = col.Format.Split('*');
double width = Convert.ToDouble(ss[0]);
double height = Convert.ToDouble(ss[1]);
builder.MoveToBookmark(item.Name);
builder.InsertImage((byte[])row[item.Name], width, height);
}
else if (col.DataType == DbType.Boolean)
{
string value = Convert.ToString(row[item.Name]);
bool bitValue = false;
if (string.IsNullOrEmpty(value) == false) bitValue = Convert.ToBoolean(value);
if (bitValue == true) { value = "是"; }
else { value = "否"; }
item.Text = value;
}
else if (col.Format != "")
{
item.Text = DataConvert.Format(Convert.ToString(row[item.Name]), col.Format);
}
else
{
item.Text = Convert.ToString(row[item.Name]);
}
}
if (index > 0) dstdoc.AppendDocument(doc, ImportFormatMode.KeepSourceFormatting);
}
string dir = AppDomain.CurrentDomain.BaseDirectory + "ServerCookies";
if (Directory.Exists(dir) == false) Directory.CreateDirectory(dir);
string fileName = "ServerCookies/" + Guid.NewGuid().ToString() + ".doc";
string TempFilePath = AppDomain.CurrentDomain.BaseDirectory + fileName;
dstdoc.Save(TempFilePath);
return fileName;
}
#endregion
#region 导出Word模板替换
public string Export<T>(string WordTemplatePath, List<T> ListModel, TableColumnCollection listColumns)
{
if (WordTemplatePath.IndexOf(":") == -1) WordTemplatePath = AppDomain.CurrentDomain.BaseDirectory + WordTemplatePath;
Aspose.Words.Document dstdoc = new Aspose.Words.Document(WordTemplatePath);
Aspose.Words.Document doc = null;
Type t = typeof(T);
foreach (var row in ListModel)
{
int index = ListModel.IndexOf(row);
if (index == 0)
{
doc = dstdoc;
}
else
{
doc = new Aspose.Words.Document(WordTemplatePath);
doc.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;
}
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder();
builder.Document = doc;
foreach (Bookmark item in doc.Range.Bookmarks)
{
var col = listColumns.GetTableColumn(item.Name);
if (col == null) continue;
if (col.KeyValueFormat.Count > 0)
{
string v = Convert.ToString(t.GetProperty(item.Name).GetValue(row, null));
foreach (var Keyitem in col.KeyValueFormat)
{
if (Keyitem.Key == v) item.Text = Keyitem.Value;
}
}
else if (col.DataType == DbType.Object)
{
string[] ss = col.Format.Split('*');
double width = Convert.ToDouble(ss[0]);
double height = Convert.ToDouble(ss[1]);
string filePath = Convert.ToString(t.GetProperty(item.Name).GetValue(row, null));
if (filePath.IndexOf(":") == -1) filePath = AppDomain.CurrentDomain.BaseDirectory + filePath;
if (System.IO.File.Exists(filePath) == true)
{
builder.MoveToBookmark(item.Name);
builder.InsertImage(filePath, width, height);
}
}
else if (col.DataType == DbType.Binary)
{
string[] ss = col.Format.Split('*');
double width = Convert.ToDouble(ss[0]);
double height = Convert.ToDouble(ss[1]);
builder.MoveToBookmark(item.Name);
builder.InsertImage((byte[])t.GetProperty(item.Name).GetValue(row, null), width, height);
}
else if (col.DataType == DbType.Boolean)
{
string value = Convert.ToString(t.GetProperty(item.Name).GetValue(row, null));
bool bitValue = false;
if (string.IsNullOrEmpty(value) == false) bitValue = Convert.ToBoolean(value);
if (bitValue == true) { value = "是"; }
else { value = "否"; }
item.Text = value;
}
else if (col.Format != "")
{
item.Text = DataConvert.Format(Convert.ToString(t.GetProperty(item.Name).GetValue(row, null)), col.Format);
}
else if (item.Name != null)
{
item.Text = Convert.ToString(t.GetProperty(item.Name).GetValue(row, null));
}
}
if (index > 0) dstdoc.AppendDocument(doc, ImportFormatMode.KeepSourceFormatting);
}
string dir = AppDomain.CurrentDomain.BaseDirectory + "ServerCookies";
if (Directory.Exists(dir) == false) Directory.CreateDirectory(dir);
string fileName = "ServerCookies/" + Guid.NewGuid().ToString() + ".doc";
string TempFilePath = AppDomain.CurrentDomain.BaseDirectory + fileName;
dstdoc.Save(TempFilePath);
return fileName;
}
#endregion
#region 导出模板替换
public string Export(string WordTemplatePath, DataTable source, TableColumnCollection listColumns, String SaveFileExtName)
{
if (WordTemplatePath.IndexOf(":") == -1) WordTemplatePath = AppDomain.CurrentDomain.BaseDirectory + WordTemplatePath;
Aspose.Words.Document dstdoc = new Aspose.Words.Document(WordTemplatePath);
Aspose.Words.Document doc = null;
foreach (DataRow row in source.Rows)
{
int index = source.Rows.IndexOf(row);
if (index == 0)
{
doc = dstdoc;
}
else
{
doc = new Aspose.Words.Document(WordTemplatePath);
doc.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;
}
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder();
builder.Document = doc;
foreach (Bookmark item in doc.Range.Bookmarks)
{
var col = listColumns.GetTableColumn(item.Name);
if (col == null) continue;
if (col.KeyValueFormat.Count > 0)
{
string v = Convert.ToString(row[item.Name]);
foreach (var Keyitem in col.KeyValueFormat)
{
if (Keyitem.Key == v) item.Text = Keyitem.Value;
}
}
else if (col.DataType == DbType.Object)
{
string[] ss = col.Format.Split('*');
double width = Convert.ToDouble(ss[0]);
double height = Convert.ToDouble(ss[1]);
string filePath = Convert.ToString(row[item.Name]);
if (filePath.IndexOf(":") == -1) filePath = AppDomain.CurrentDomain.BaseDirectory + filePath;
//ErrorFollow.TraceWrite("导出word照片", "", filePath);
if (System.IO.File.Exists(filePath) == true)
{
builder.MoveToBookmark(item.Name);
builder.InsertImage(filePath, width, height);
}
}
else if (col.DataType == DbType.Binary)
{
string[] ss = col.Format.Split('*');
double width = Convert.ToDouble(ss[0]);
double height = Convert.ToDouble(ss[1]);
builder.MoveToBookmark(item.Name);
builder.InsertImage((byte[])row[item.Name], width, height);
}
else if (col.DataType == DbType.Boolean)
{
string value = Convert.ToString(row[item.Name]);
bool bitValue = false;
if (string.IsNullOrEmpty(value) == false) bitValue = Convert.ToBoolean(value);
if (bitValue == true) { value = "是"; }
else { value = "否"; }
item.Text = value;
}
else if (col.Format != "")
{
item.Text = DataConvert.Format(Convert.ToString(row[item.Name]), col.Format);
}
else
{
item.Text = Convert.ToString(row[item.Name]);
}
}
if (index > 0) dstdoc.AppendDocument(doc, ImportFormatMode.KeepSourceFormatting);
}
string dir = AppDomain.CurrentDomain.BaseDirectory + "ServerCookies";
if (Directory.Exists(dir) == false) Directory.CreateDirectory(dir);
string fileName = "ServerCookies/" + Guid.NewGuid().ToString() + SaveFileExtName;
string TempFilePath = AppDomain.CurrentDomain.BaseDirectory + fileName;
switch (SaveFileExtName)
{
case ".pdf":
dstdoc.Save(TempFilePath, SaveFormat.Pdf);
break;
default:
dstdoc.Save(TempFilePath);
break;
}
return fileName;
}
#endregion
#region 导出Word模板替换
public string Export<T>(string WordTemplatePath, List<T> ListModel, TableColumnCollection listColumns, String SaveFileExtName)
{
if (WordTemplatePath.IndexOf(":") == -1) WordTemplatePath = AppDomain.CurrentDomain.BaseDirectory + WordTemplatePath;
Aspose.Words.Document dstdoc = new Aspose.Words.Document(WordTemplatePath);
Aspose.Words.Document doc = null;
Type t = typeof(T);
foreach (var row in ListModel)
{
int index = ListModel.IndexOf(row);
if (index == 0)
{
doc = dstdoc;
}
else
{
doc = new Aspose.Words.Document(WordTemplatePath);
doc.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;
}
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder();
builder.Document = doc;
foreach (Bookmark item in doc.Range.Bookmarks)
{
var col = listColumns.GetTableColumn(item.Name);
if (col == null) continue;
if (col.KeyValueFormat.Count > 0)
{
string v = Convert.ToString(t.GetProperty(item.Name).GetValue(row, null));
foreach (var Keyitem in col.KeyValueFormat)
{
if (Keyitem.Key == v) item.Text = Keyitem.Value;
}
}
else if (col.DataType == DbType.Object)
{
string[] ss = col.Format.Split('*');
double width = Convert.ToDouble(ss[0]);
double height = Convert.ToDouble(ss[1]);
string filePath = Convert.ToString(t.GetProperty(item.Name).GetValue(row, null));
if (filePath.IndexOf(":") == -1) filePath = AppDomain.CurrentDomain.BaseDirectory + filePath;
if (System.IO.File.Exists(filePath) == true)
{
builder.MoveToBookmark(item.Name);
builder.InsertImage(filePath, width, height);
}
}
else if (col.DataType == DbType.Binary)
{
string[] ss = col.Format.Split('*');
double width = Convert.ToDouble(ss[0]);
double height = Convert.ToDouble(ss[1]);
builder.MoveToBookmark(item.Name);
builder.InsertImage((byte[])t.GetProperty(item.Name).GetValue(row, null), width, height);
}
else if (col.DataType == DbType.Boolean)
{
string value = Convert.ToString(t.GetProperty(item.Name).GetValue(row, null));
bool bitValue = false;
if (string.IsNullOrEmpty(value) == false) bitValue = Convert.ToBoolean(value);
if (bitValue == true) { value = "是"; }
else { value = "否"; }
item.Text = value;
}
else if (col.Format != "")
{
item.Text = DataConvert.Format(Convert.ToString(t.GetProperty(item.Name).GetValue(row, null)), col.Format);
}
else
{
item.Text = Convert.ToString(t.GetProperty(item.Name).GetValue(row, null));
}
}
if (index > 0) dstdoc.AppendDocument(doc, ImportFormatMode.KeepSourceFormatting);
}
string dir = AppDomain.CurrentDomain.BaseDirectory + "ServerCookies";
if (Directory.Exists(dir) == false) Directory.CreateDirectory(dir);
string fileName = "ServerCookies/" + Guid.NewGuid().ToString() + SaveFileExtName;
string TempFilePath = AppDomain.CurrentDomain.BaseDirectory + fileName;
switch (SaveFileExtName)
{
case ".pdf":
dstdoc.Save(TempFilePath, SaveFormat.Pdf);
break;
default:
dstdoc.Save(TempFilePath);
break;
}
return fileName;
}
#endregion
#region 导出Word模板替换
public string Export(string WordTemplatePath, List<Dictionary<string, string>> ListModel, String SaveFileExtName)
{
if (WordTemplatePath.IndexOf(":") == -1) WordTemplatePath = AppDomain.CurrentDomain.BaseDirectory + WordTemplatePath;
Aspose.Words.Document dstdoc = new Aspose.Words.Document(WordTemplatePath);
Aspose.Words.Document doc = null;
foreach (var row in ListModel)
{
int index = ListModel.IndexOf(row);
if (index == 0)
{
doc = dstdoc;
}
else
{
doc = new Aspose.Words.Document(WordTemplatePath);
doc.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;
}
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder();
builder.Document = doc;
foreach (Bookmark item in doc.Range.Bookmarks)
{
var col = row.ContainsKey(item.Name);
if (col == false) continue;
item.Text = row[item.Name];
}
if (index > 0) dstdoc.AppendDocument(doc, ImportFormatMode.KeepSourceFormatting);
}
string dir = AppDomain.CurrentDomain.BaseDirectory + "ServerCookies";
if (Directory.Exists(dir) == false) Directory.CreateDirectory(dir);
string fileName = "ServerCookies/" + Guid.NewGuid().ToString() + SaveFileExtName;
string TempFilePath = AppDomain.CurrentDomain.BaseDirectory + fileName;
switch (SaveFileExtName)
{
case ".pdf":
dstdoc.Save(TempFilePath, SaveFormat.Pdf);
break;
default:
dstdoc.Save(TempFilePath);
break;
}
return fileName;
}
#endregion
#region 添加空格
public string Space(int len)
{
string ss = "";
for (int i = 0; i < len; i++)
{
ss += " ";
}
return ss;
}
#endregion
#region Word打印
public void Print(WordReport report, string PrinterName)
{
Aspose.Words.Document doc = new Aspose.Words.Document();
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder();
builder.Document = doc;
foreach (var item in report.ListTables)
{
foreach (var item2 in item.ListTexts)
{
builder.Font.Name = item2.FontName;
builder.Font.Size = item2.FontSize;
builder.Font.Bold = item2.FontBold;
builder.Font.Color = item2.FontColor;
//builder.ParagraphFormat.Shading.BackgroundPatternColor= item2.Background;
if (item2.TextAlign == "Center") builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Center;//水平居中对齐
if (item2.TextAlign == "Right") builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Right;//水平居右对齐
if (item2.TextAlign == "Left") builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Left;//水平居右对齐
builder.Writeln(item2.Text);
}
item.MathMerge(); //计算合并单元格
Aspose.Words.Tables.Table table = builder.StartTable();
foreach (var row in item.Rows)
{
foreach (var cell in row)
{
builder.InsertCell();
builder.CellFormat.Borders.LineStyle = Aspose.Words.LineStyle.Single;
builder.CellFormat.Borders.Color = System.Drawing.Color.Black;
builder.CellFormat.HorizontalMerge = (Aspose.Words.Tables.CellMerge)cell.HorizontalCellMerge;
builder.CellFormat.VerticalMerge = (Aspose.Words.Tables.CellMerge)cell.VerticalCellMerge;
//单元格背景色
builder.CellFormat.Shading.BackgroundPatternColor = cell.Background;
if (cell.VerticalAlign == "Top") builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Top;//垂直居中对齐
if (cell.VerticalAlign == "Center") builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;//垂直居中对齐
if (cell.VerticalAlign == "Bottom") builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Bottom;//垂直居中对齐
//builder.Font.Shading.BackgroundPatternColor = Color.Red;
if (cell.TextAlign == "Center")
{
builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Center;//水平居中对齐
//builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;//垂直居中对齐
}
if (cell.TextAlign == "Right") builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Right;
if (cell.TextAlign == "Left") builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Left;
builder.CellFormat.Width = cell.Width;
if (cell.Height > 0) builder.RowFormat.Height = cell.Height;
builder.Font.Name = cell.FontName;
builder.Font.Size = cell.FontSize;
builder.Font.Color = cell.FontColor;
builder.Font.Bold = cell.FontBold;
if (cell.Text == null) cell.Text = "";
switch (cell.ContentType)
{
case WordCellType.None:
builder.Write(cell.Text);
break;
case WordCellType.ImagePath:
if (System.IO.File.Exists(cell.Text) == true)
{
builder.InsertImage(cell.Text, cell.ImageWidth, cell.ImageHeight);
}
else { builder.Write(""); }
break;
case WordCellType.ImageByte:
builder.InsertImage(cell.ImageBuffer, cell.ImageWidth, cell.ImageHeight);
break;
case WordCellType.Image:
builder.InsertImage(cell.ImagePhoto, cell.ImageWidth, cell.ImageHeight);
break;
default:
break;
}
}
builder.EndRow();
}
builder.EndTable();
foreach (var item2 in item.ListFooterTexts)
{
builder.Font.Name = item2.FontName;
builder.Font.Size = item2.FontSize;
builder.Font.Bold = item2.FontBold;
builder.Font.Color = item2.FontColor;
//builder.ParagraphFormat.Shading.BackgroundPatternColor= item2.Background;
if (item2.TextAlign == "Center") builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Center;//水平居中对齐
if (item2.TextAlign == "Right") builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Right;//水平居右对齐
if (item2.TextAlign == "Left") builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Left;//水平居右对齐
builder.Writeln(item2.Text);
}
//table.AutoFit(Aspose.Words.Tables.AutoFitBehavior.FixedColumnWidths);
}
//string fileName = "ServerCookies/" + Guid.NewGuid().ToString() + ".doc";
//string TempFilePath = AppDomain.CurrentDomain.BaseDirectory + fileName;
//doc.Save(TempFilePath);
if (string.IsNullOrEmpty(PrinterName) == true) { doc.Print(); }
else { doc.Print(PrinterName); }
}
#endregion
#region Word打印_多页
public void Export(List<WordReport> ListReport, string PrinterName)
{
Aspose.Words.Document dstdoc = new Aspose.Words.Document();
Aspose.Words.Document doc = null;
foreach (WordReport report in ListReport)
{
int index = ListReport.IndexOf(report);
if (index == 0)
{
doc = dstdoc;
}
else
{
doc = new Document();
doc.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;
}
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder();
builder.Document = doc;
foreach (var item in report.ListTables)
{
foreach (var item2 in item.ListTexts)
{
builder.Font.Name = item2.FontName;
builder.Font.Size = item2.FontSize;
builder.Font.Bold = item2.FontBold;
builder.Font.Color = item2.FontColor;
//builder.ParagraphFormat.Shading.BackgroundPatternColor= item2.Background;
if (item2.TextAlign == "Center") builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Center;//水平居中对齐
if (item2.TextAlign == "Right") builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Right;//水平居右对齐
if (item2.TextAlign == "Left") builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Left;//水平居右对齐
builder.Writeln(item2.Text);
}
item.MathMerge(); //计算合并单元格
Aspose.Words.Tables.Table table = builder.StartTable();
foreach (var row in item.Rows)
{
foreach (var cell in row)
{
builder.InsertCell();
builder.CellFormat.Borders.LineStyle = Aspose.Words.LineStyle.Single;
builder.CellFormat.Borders.Color = System.Drawing.Color.Black;
builder.CellFormat.HorizontalMerge = (Aspose.Words.Tables.CellMerge)cell.HorizontalCellMerge;
builder.CellFormat.VerticalMerge = (Aspose.Words.Tables.CellMerge)cell.VerticalCellMerge;
//单元格背景色
builder.CellFormat.Shading.BackgroundPatternColor = cell.Background;
if (cell.VerticalAlign == "Top") builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Top;//垂直居中对齐
if (cell.VerticalAlign == "Center") builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;//垂直居中对齐
if (cell.VerticalAlign == "Bottom") builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Bottom;//垂直居中对齐
//builder.Font.Shading.BackgroundPatternColor = Color.Red;
if (cell.TextAlign == "Center")
{
builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Center;//水平居中对齐
//builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;//垂直居中对齐
}
if (cell.TextAlign == "Right") builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Right;
if (cell.TextAlign == "Left") builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Left;
builder.CellFormat.Width = cell.Width;
if (cell.Height > 0) builder.RowFormat.Height = cell.Height;
builder.Font.Name = cell.FontName;
builder.Font.Size = cell.FontSize;
builder.Font.Color = cell.FontColor;
builder.Font.Bold = cell.FontBold;
if (cell.Text == null) cell.Text = "";
switch (cell.ContentType)
{
case WordCellType.None:
builder.Write(cell.Text);
break;
case WordCellType.ImagePath:
if (System.IO.File.Exists(cell.Text) == true)
{
builder.InsertImage(cell.Text, cell.ImageWidth, cell.ImageHeight);
}
else { builder.Write(""); }
break;
case WordCellType.ImageByte:
builder.InsertImage(cell.ImageBuffer, cell.ImageWidth, cell.ImageHeight);
break;
case WordCellType.Image:
builder.InsertImage(cell.ImagePhoto, cell.ImageWidth, cell.ImageHeight);
break;
default:
break;
}
}
builder.EndRow();
}
builder.EndTable();
foreach (var item2 in item.ListFooterTexts)
{
builder.Font.Name = item2.FontName;
builder.Font.Size = item2.FontSize;
builder.Font.Bold = item2.FontBold;
builder.Font.Color = item2.FontColor;
//builder.ParagraphFormat.Shading.BackgroundPatternColor= item2.Background;
if (item2.TextAlign == "Center") builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Center;//水平居中对齐
if (item2.TextAlign == "Right") builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Right;//水平居右对齐
if (item2.TextAlign == "Left") builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Left;//水平居右对齐
builder.Writeln(item2.Text);
}
}
if (index > 0) dstdoc.AppendDocument(doc, ImportFormatMode.KeepSourceFormatting);
}
if (string.IsNullOrEmpty(PrinterName) == true) { dstdoc.Print(); }
else { dstdoc.Print(PrinterName); }
//string fileName = "ServerCookies/" + Guid.NewGuid().ToString() + ".doc";
//string TempFilePath = AppDomain.CurrentDomain.BaseDirectory + fileName;
//dstdoc.Save(TempFilePath);
//return fileName;
}
#endregion
#region Word打印模板替换
public void Print(string WordTemplatePath, DataTable source, TableColumnCollection listColumns, string PrinterName)
{
if (WordTemplatePath.IndexOf(":") == -1) WordTemplatePath = AppDomain.CurrentDomain.BaseDirectory + WordTemplatePath;
Aspose.Words.Document dstdoc = new Aspose.Words.Document(WordTemplatePath);
Aspose.Words.Document doc = null;
foreach (DataRow row in source.Rows)
{
int index = source.Rows.IndexOf(row);
if (index == 0)
{
doc = dstdoc;
}
else
{
doc = new Aspose.Words.Document(WordTemplatePath);
doc.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;
}
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder();
builder.Document = doc;
foreach (Bookmark item in doc.Range.Bookmarks)
{
var col = listColumns.GetTableColumn(item.Name);
if (col == null) continue;
if (col.KeyValueFormat.Count > 0)
{
string v = Convert.ToString(row[item.Name]);
foreach (var Keyitem in col.KeyValueFormat)
{
if (Keyitem.Key == v) item.Text = Keyitem.Value;
}
}
else if (col.DataType == DbType.Object)
{
string[] ss = col.Format.Split('*');
double width = Convert.ToDouble(ss[0]);
double height = Convert.ToDouble(ss[1]);
string filePath = Convert.ToString(row[item.Name]);
if (filePath.IndexOf(":") == -1) filePath = AppDomain.CurrentDomain.BaseDirectory + filePath;
//ErrorFollow.TraceWrite("导出word照片", "", filePath);
if (System.IO.File.Exists(filePath) == true)
{
builder.MoveToBookmark(item.Name);
builder.InsertImage(filePath, width, height);
}
}
else if (col.DataType == DbType.Binary)
{
string[] ss = col.Format.Split('*');
double width = Convert.ToDouble(ss[0]);
double height = Convert.ToDouble(ss[1]);
builder.MoveToBookmark(item.Name);
builder.InsertImage((byte[])row[item.Name], width, height);
}
else if (col.DataType == DbType.Boolean)
{
string value = Convert.ToString(row[item.Name]);
bool bitValue = false;
if (string.IsNullOrEmpty(value) == false) bitValue = Convert.ToBoolean(value);
if (bitValue == true) { value = "是"; }
else { value = "否"; }
item.Text = value;
}
else if (col.Format != "")
{
item.Text = DataConvert.Format(Convert.ToString(row[item.Name]), col.Format);
}
else
{
item.Text = Convert.ToString(row[item.Name]);
}
}
if (index > 0) dstdoc.AppendDocument(doc, ImportFormatMode.KeepSourceFormatting);
}
if (string.IsNullOrEmpty(PrinterName) == true) { doc.Print(); }
else { doc.Print(PrinterName); }
//string fileName = "ServerCookies/" + Guid.NewGuid().ToString() + ".doc";
//string TempFilePath = AppDomain.CurrentDomain.BaseDirectory + fileName;
//dstdoc.Save(TempFilePath);
//return fileName;
}
#endregion
#region Word打印模板替换
public void Print<T>(string WordTemplatePath, List<T> ListModel, TableColumnCollection listColumns, string PrinterName)
{
if (WordTemplatePath.IndexOf(":") == -1) WordTemplatePath = AppDomain.CurrentDomain.BaseDirectory + WordTemplatePath;
Aspose.Words.Document dstdoc = new Aspose.Words.Document(WordTemplatePath);
//PageSetup ps = builder.PageSetup;
//PageSetup ps = doc.Sections[0].PageSetup;
//ps.LeftMargin = report.Left;//设置或修改边距
//ps.TopMargin = report.Top;//设置或修改边距
//ps.RightMargin = report.Right;//设置或修改边距
//ps.BottomMargin = report.Bottom;//设置或修改边距
//ps.PaperSize = (PaperSize)report.PageType; //纸张类型
//ps.Orientation = (Orientation)report.OrientationType; //纸张方向
Aspose.Words.Document doc = null;
Type t = typeof(T);
foreach (var row in ListModel)
{
int index = ListModel.IndexOf(row);
if (index == 0)
{
doc = dstdoc;
//PageSetup ps = doc.Sections[0].PageSetup;
//ps.LeftMargin = report.Left;//设置或修改边距
//ps.TopMargin = report.Top;//设置或修改边距
//ps.RightMargin = report.Right;//设置或修改边距
//ps.BottomMargin = report.Bottom;//设置或修改边距
//ps.PaperSize = PaperSize.Custom; //纸张类型
//ps.Orientation = (Orientation)report.OrientationType; //纸张方向
//ps.PageWidth = 9.8;
//ps.PageHeight = 21;
}
else
{
doc = new Aspose.Words.Document(WordTemplatePath);
doc.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;
}
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder();
builder.Document = doc;
foreach (Bookmark item in doc.Range.Bookmarks)
{
var col = listColumns.GetTableColumn(item.Name);
if (col == null) continue;
if (col.KeyValueFormat.Count > 0)
{
string v = Convert.ToString(t.GetProperty(item.Name).GetValue(row, null));
foreach (var Keyitem in col.KeyValueFormat)
{
if (Keyitem.Key == v) item.Text = Keyitem.Value;
}
}
else if (col.DataType == DbType.Object)
{
string[] ss = col.Format.Split('*');
double width = Convert.ToDouble(ss[0]);
double height = Convert.ToDouble(ss[1]);
string filePath = Convert.ToString(t.GetProperty(item.Name).GetValue(row, null));
if (filePath.IndexOf(":") == -1) filePath = AppDomain.CurrentDomain.BaseDirectory + filePath;
if (System.IO.File.Exists(filePath) == true)
{
builder.MoveToBookmark(item.Name);
builder.InsertImage(filePath, width, height);
}
}
else if (col.DataType == DbType.Binary)
{
string[] ss = col.Format.Split('*');
double width = Convert.ToDouble(ss[0]);
double height = Convert.ToDouble(ss[1]);
builder.MoveToBookmark(item.Name);
builder.InsertImage((byte[])t.GetProperty(item.Name).GetValue(row, null), width, height);
}
else if (col.DataType == DbType.Boolean)
{
string value = Convert.ToString(t.GetProperty(item.Name).GetValue(row, null));
bool bitValue = false;
if (string.IsNullOrEmpty(value) == false) bitValue = Convert.ToBoolean(value);
if (bitValue == true) { value = "是"; }
else { value = "否"; }
item.Text = value;
}
else if (col.Format != "")
{
item.Text = DataConvert.Format(Convert.ToString(t.GetProperty(item.Name).GetValue(row, null)), col.Format);
}
else
{
item.Text = Convert.ToString(t.GetProperty(item.Name).GetValue(row, null));
}
}
if (index > 0) dstdoc.AppendDocument(doc, ImportFormatMode.KeepSourceFormatting);
}
//string fileName = "ServerCookies/" + Guid.NewGuid().ToString() + ".pdf";
//string TempFilePath = AppDomain.CurrentDomain.BaseDirectory + fileName;
//dstdoc.Save(TempFilePath, SaveFormat.Pdf);
//return fileName;
if (string.IsNullOrEmpty(PrinterName) == true) { doc.Print(); }
else { doc.Print(PrinterName); }
}
#endregion
#region Word打印模板替换
public void Print<T>(string WordTemplatePath, T Model, TableColumnCollection listColumns, string PrinterName)
{
if (WordTemplatePath.IndexOf(":") == -1) WordTemplatePath = AppDomain.CurrentDomain.BaseDirectory + WordTemplatePath;
Aspose.Words.Document doc = new Aspose.Words.Document(WordTemplatePath);
Type t = typeof(T);
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder();
builder.Document = doc;
foreach (Bookmark item in doc.Range.Bookmarks)
{
var col = listColumns.GetTableColumn(item.Name);
if (col == null) continue;
if (col.KeyValueFormat.Count > 0)
{
string v = Convert.ToString(t.GetProperty(item.Name).GetValue(Model, null));
foreach (var Keyitem in col.KeyValueFormat)
{
if (Keyitem.Key == v) item.Text = Keyitem.Value;
}
}
else if (col.DataType == DbType.Object)
{
string[] ss = col.Format.Split('*');
double width = Convert.ToDouble(ss[0]);
double height = Convert.ToDouble(ss[1]);
string filePath = Convert.ToString(t.GetProperty(item.Name).GetValue(Model, null));
if (filePath.IndexOf(":") == -1) filePath = AppDomain.CurrentDomain.BaseDirectory + filePath;
if (System.IO.File.Exists(filePath) == true)
{
builder.MoveToBookmark(item.Name);
builder.InsertImage(filePath, width, height);
}
}
else if (col.DataType == DbType.Binary)
{
string[] ss = col.Format.Split('*');
double width = Convert.ToDouble(ss[0]);
double height = Convert.ToDouble(ss[1]);
builder.MoveToBookmark(item.Name);
builder.InsertImage((byte[])t.GetProperty(item.Name).GetValue(Model, null), width, height);
}
else if (col.DataType == DbType.Boolean)
{
string value = Convert.ToString(t.GetProperty(item.Name).GetValue(Model, null));
bool bitValue = false;
if (string.IsNullOrEmpty(value) == false) bitValue = Convert.ToBoolean(value);
if (bitValue == true) { value = "是"; }
else { value = "否"; }
item.Text = value;
}
else if (col.Format != "")
{
item.Text = DataConvert.Format(Convert.ToString(t.GetProperty(item.Name).GetValue(Model, null)), col.Format);
}
else
{
item.Text = Convert.ToString(t.GetProperty(item.Name).GetValue(Model, null));
}
}
if (string.IsNullOrEmpty(PrinterName) == true) { doc.Print(); }
else { doc.Print(PrinterName); }
}
#endregion
}
#region Word报表
public class WordReport
{
public double Left { get; set; }
public double Top { get; set; }
public double Right { get; set; }
public double Bottom { get; set; }
public WordPage PageType { get; set; }
public WordPageOrientation OrientationType { get; set; }
public List<WordTable> ListTables { get; set; }
public WordReport()
{
ListTables = new List<WordTable>();
PageType = WordPage.A4;
OrientationType = WordPageOrientation.Portrait;
Top = 20;
Left = 20;
Right = 20;
Bottom = 20;
}
}
#endregion
#region Word表格
public class WordTable
{
public List<List<WordCell>> Rows { get; set; }
public List<WordText> ListTexts { get; set; }
public List<WordText> ListFooterTexts { get; set; }
public WordTable()
{
Rows = new List<List<WordCell>>();
ListTexts = new List<WordText>();
ListFooterTexts = new List<WordText>();
}
#region 计算单元格合并
public void MathMerge()
{
foreach (List<WordCell> row in this.Rows)
{
int rowIndex = this.Rows.IndexOf(row);
for (int i = 0; i < row.Count; i++)
{
WordCell cell = row[i];
if (cell.RowSpan > 1)
{ //如果跨行数等于>1,那么添加,计算本单元格所需高度
cell.VerticalCellMerge = 1;
for (int ii = 1; ii < cell.RowSpan; ii++)
{
this.Rows[rowIndex + ii][i].VerticalCellMerge = 2;
}
}
if (cell.ColumnSpan > 1)
{ //如果跨列数大于1那么计算本单元格所需宽度
cell.HorizontalCellMerge = 1;
for (int ii = 1; ii < cell.ColumnSpan; ii++)
{
row[i + ii].HorizontalCellMerge = 2;
//cell.Width += row[i + ii].Width;
}
}
//如果不是第一行,并且左边与上边的单元格属性为隐藏,那么隐藏单元格
if (rowIndex > 0 && i > 0)
{
if (row[i - 1].HorizontalCellMerge > 0 && this.Rows[rowIndex - 1][i].VerticalCellMerge > 0)
{
if (row[i - 1].VerticalCellMerge == 2) cell.HorizontalCellMerge = 2;
cell.VerticalCellMerge = 2;
}
}
}
}
}
#endregion
}
#endregion
#region Word单元格
public class WordCell
{
public double Width { get; set; }
public double Height { get; set; }
public double ImageWidth { get; set; }
public double ImageHeight { get; set; }
public byte[] ImageBuffer { get; set; }
public Image ImagePhoto { get; set; }
public int ColumnSpan { get; set; }
public int RowSpan { get; set; }
public string Text { get; set; }
public WordCellType ContentType { get; set; }
public string FontName { get; set; }
public double FontSize { get; set; }
public bool FontBold { get; set; }
public string TextAlign { get; set; }
public string VerticalAlign { get; set; }
public Color FontColor { get; set; }
public Color Background { get; set; }
public int HorizontalCellMerge { get; set; }
public int VerticalCellMerge { get; set; }
public WordCell()
{
ContentType = WordCellType.None;
FontName = "宋体";
FontSize = 9;
FontColor = Color.Black;
TextAlign = "Center";
VerticalAlign = "Center"; //Bottom,Top,Center
Text = "";
}
}
#endregion
#region Word单元格类型
public enum WordCellType
{
None,
ImagePath,
ImageByte,
Image
}
#endregion
#region Word纸张
public enum WordPage
{
/// <summary>
/// 宽595*高848
/// </summary>
A4 = 1,
A3 = 0
}
#endregion
#region Word纸张方向
public enum WordPageOrientation
{
Portrait = 1,
Landscape = 2
}
#endregion
#region Word行字符串
public class WordText
{
public string Text { get; set; }
public string FontName { get; set; }
public double FontSize { get; set; }
public bool FontBold { get; set; }
public Color FontColor { get; set; }
public string TextAlign { get; set; }
public Color Background { get; set; }
public WordText()
{
FontName = "宋体";
FontSize = 9;
FontColor = Color.Black;
Background = Color.White;
TextAlign = "Center";
Text = "";
}
public WordText(string Text, double FontSize, string TextAlign)
{
this.Text = Text;
this.FontSize = FontSize;
this.TextAlign = TextAlign;
FontName = "宋体";
FontColor = Color.Black;
Background = Color.White;
}
public WordText(string Text, double FontSize, string TextAlign, bool FontBold)
{
this.Text = Text;
this.FontSize = FontSize;
this.TextAlign = TextAlign;
FontName = "宋体";
FontColor = Color.Black;
Background = Color.White;
this.FontBold = FontBold;
}
}
#endregion
}