Commit e8c7b1ee authored by 黄奎's avatar 黄奎

页面修改

parent 1faf7826
......@@ -22,6 +22,9 @@
<Reference Include="Aspose.Cells">
<HintPath>..\lib\Aspose.Cells.dll</HintPath>
</Reference>
<Reference Include="Aspose.Words">
<HintPath>..\..\think_project2\Api\lib\18.7\Aspose.Words.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
using NPOI.OpenXmlFormats.Wordprocessing;
using NPOI.XWPF.UserModel;
using System.Collections.Generic;
using Aspose.Words;
using System.IO;
using System.Net;
namespace Edu.Common.Plugin
{
......@@ -10,258 +9,102 @@ namespace Edu.Common.Plugin
/// </summary>
public class WordTemplateHelper
{
#region 网页内容或导入wordexcel
/// <summary>
/// 下载Word表格
/// 网页内容保存或导出为word或excel
/// </summary>
/// <param name="list">数据源</param>
/// <param name="list2">第二张表格</param>
/// <param name="isTemplate">true-模板导出,false-不是模板导出</param>
/// <param name="templatePath">模板文件路劲</param>
/// <param name="url">网页地址</param>
/// <param name="templatePath">模板文件</param>
/// <param name="newFilePath">新文件保存路径</param>
/// <returns></returns>
public static byte[] GuestListToWord(List<WordData> list,List<WordData> list2, bool isTemplate = false, string templatePath = "")
public static bool UrlToWord(string url, string templatePath, string newFilePath)
{
var doc = CreateXWPFDocument(isTemplate, templatePath);
if (list != null && list.Count > 0)
FileStream pFileStream = null;
try
{
int rowCount = list.Count;
int colCount = list[0].WordRow.Count;
XWPFTable table = doc.CreateTable(rowCount + 2, colCount);
XWPFParagraph xp = doc.CreateParagraph();
xp.Alignment = ParagraphAlignment.CENTER;
XWPFRun xr = xp.CreateRun();
CT_RPr rpr = xr.GetCTR().AddNewRPr();
CT_Fonts rfonts = rpr.AddNewRFonts();
rfonts.ascii = "宋体";
rfonts.eastAsia = "宋体";
rpr.AddNewSz().val = (ulong)21;//5号字体
rpr.AddNewSzCs().val = (ulong)21;
WebRequest req = WebRequest.Create(url);
WebResponse resp = req.GetResponse();
StreamReader sr = new StreamReader(resp.GetResponseStream(), System.Text.Encoding.UTF8);
string htmlContent = sr.ReadToEnd();
Aspose.Words.Document doc = new Aspose.Words.Document(templatePath);
DocumentBuilder builder = new DocumentBuilder(doc);
doc.NodeChangingCallback = new HandleNodeChangingFontChanger("宋体");
builder.InsertHtml(htmlContent);
builder.PageSetup.PaperSize = PaperSize.A4;
builder.PageSetup.BottomMargin = 0;
builder.PageSetup.TopMargin = 0;
builder.PageSetup.LeftMargin = 20;
builder.PageSetup.RightMargin = 20;
NodeCollection allTables = doc.GetChildNodes(NodeType.Table, true);
MemoryStream firstStream = new MemoryStream();
doc.Save(firstStream, SaveFormat.Doc);
var pReadByte = firstStream.GetBuffer();
pFileStream = new FileStream(newFilePath, FileMode.OpenOrCreate);
pFileStream.Write(pReadByte, 0, pReadByte.Length);
}
catch
{
return false;
}
finally
{
if (pFileStream != null)
pFileStream.Close();
}
return true;
}
table.Width = 600 * 9;
var rootRow = table.GetRow(0);
table.SetColumnWidth(0, 150);/* 设置列宽 */
table.SetColumnWidth(1, 250);
table.SetColumnWidth(2, 200);
table.SetColumnWidth(3, 200);
table.SetColumnWidth(4, 200);
table.SetColumnWidth(5, 200);
table.SetColumnWidth(6, 250);
table.SetColumnWidth(7, 250);
table.SetColumnWidth(8, 200);
table.GetRow(0).GetCell(0).SetText("序号");
table.GetRow(0).GetCell(1).SetText("姓名");
table.GetRow(0).GetCell(2).SetText("姓名");
table.GetRow(0).GetCell(3).SetText("性别");
table.GetRow(0).GetCell(4).SetText("出生日期");
table.GetRow(0).GetCell(5).SetText("护照号码");
table.GetRow(0).GetCell(6).SetText("出生地");
table.GetRow(0).GetCell(7).SetText("发证机关及日期");
table.GetRow(0).GetCell(8).SetText("发证机关及日期");
table.GetRow(1).GetCell(0).SetText("序号");
table.GetRow(1).GetCell(1).SetText("中文");
table.GetRow(1).GetCell(2).SetText("汉语拼音");
table.GetRow(1).GetCell(3).SetText("性别");
table.GetRow(1).GetCell(4).SetText("出生日期");
table.GetRow(1).GetCell(5).SetText("护照号码");
table.GetRow(1).GetCell(6).SetText("出生地");
table.GetRow(1).GetCell(7).SetText("签发地");
table.GetRow(1).GetCell(8).SetText("签发日期");
/// <summary>
MergeCellsVertically(table, 0, 0, 1);
MergeCellsVertically(table, 3, 0, 1);
MergeCellsVertically(table, 4, 0, 1);
MergeCellsVertically(table, 5, 0, 1);
MergeCellsVertically(table, 6, 0, 1);
#endregion
}
rootRow.MergeCells(1, 2);
rootRow.MergeCells(6, 7);
//填写表的内容
for (int i = 0; i < list.Count; i++)
{
for (int j = 0; j < list[0].WordRow.Count; j++)
{
string value = list[i].WordRow[j].CellValue.ToString();
table.GetRow(i + 2).GetCell(j).SetText(value);
}
}
if (list2 != null && list2.Count > 0)
{
XWPFParagraph xp2 = doc.CreateParagraph();
xp2.Alignment = ParagraphAlignment.LEFT;
XWPFTable table2 = doc.CreateTable(4, 1);
table2.Width = 1500 * 1;
table2.SetColumnWidth(0, 1500);/* 设置列宽 */
for (var i = 0; i < list2.Count; i++)
{
string value = list2[i].WordRow[0].CellValue.ToString();
table2.GetRow(i).GetCell(0).SetText(value);
}
}
return GetStream(doc);
}
return null;
}
/// <summary>
/// Aspose.words insert html
/// </summary>
public class HandleNodeChangingFontChanger : INodeChangingCallback
{
private string fontName;
/// <summary>
/// 下载旅客电话名单
/// 构造函数
/// </summary>
/// <param name="dic">数据源</param>
/// <param name="rowCount">行数</param>
/// <param name="isTemplate">true-模板导出,false-不是模板导出</param>
/// <param name="templatePath">模板文件路劲</param>
/// <returns></returns>
public static byte[] GuestTelListToWord(Dictionary<int, List<WordData>> dic,int rowCount, bool isTemplate = false, string templatePath = "")
/// <param name="fontName"></param>
public HandleNodeChangingFontChanger(string fontName)
{
var doc = CreateXWPFDocument(isTemplate, templatePath);
if (dic != null && dic.Count > 0)
{
int colCount = 5;
XWPFTable table = doc.CreateTable(rowCount + 1, colCount);
XWPFParagraph xp = doc.CreateParagraph();
xp.Alignment = ParagraphAlignment.LEFT;
XWPFRun xr = xp.CreateRun();
CT_RPr rpr = xr.GetCTR().AddNewRPr();
CT_Fonts rfonts = rpr.AddNewRFonts();
rfonts.ascii = "宋体";
rfonts.eastAsia = "宋体";
rpr.AddNewSz().val = (ulong)21;//5号字体
rpr.AddNewSzCs().val = (ulong)21;
table.Width = 950 * 5;
table.SetColumnWidth(0, 100);/* 设置列宽 */
table.SetColumnWidth(1, 200);
table.SetColumnWidth(2, 200);
table.SetColumnWidth(3, 250);
table.SetColumnWidth(4, 200);
table.GetRow(0).GetCell(0).SetText("NO");
table.GetRow(0).GetCell(1).SetText("中文姓名");
table.GetRow(0).GetCell(2).SetText("电话");
table.GetRow(0).GetCell(3).SetText("业务");
table.GetRow(0).GetCell(4).SetText("备注");
//填写表的内容
int index = 1;
int rootIndex = 1;
foreach (var item in dic)
{
foreach (var subItem in item.Value)
{
for (int j = 0; j < 5; j++)
{
string value = string.Empty;
if (j == 0)
{
value = index.ToString();
}
else
{
value = subItem.WordRow[j-1].CellValue.ToString();
}
table.GetRow(index).GetCell(j).SetText(value);
}
index++;
}
//跨行合并单元格
if (item.Value.Count > 1)
{
MergeCellsVertically(table, 3, index - item.Value.Count, index-1);
}
rootIndex++;
}
return GetStream(doc);
}
return null;
this.fontName = fontName;
}
/// <summary>
/// 跨行合并
/// </summary>
/// <param name="table">表格</param>
/// <param name="col">列索引</param>
/// <param name="fromRow">起始行</param>
/// <param name="toRow">结束行</param>
private static void MergeCellsVertically(XWPFTable table, int col, int fromRow, int toRow)
void INodeChangingCallback.NodeInserted(NodeChangingArgs args)
{
for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++)
// Change the font of inserted text contained in the Run nodes.
if (args.Node.NodeType == NodeType.Run)
{
XWPFTableCell cell = table.GetRow(rowIndex).GetCell(col);
if (rowIndex == fromRow)
{
cell.GetCTTc().AddNewTcPr().AddNewVMerge().val=ST_Merge.restart;
}
else
{
cell.GetCTTc().AddNewTcPr().AddNewVMerge().val= ST_Merge.@continue;
}
Aspose.Words.Font font = ((Run)args.Node).Font;
font.Name = fontName;
string demo = ((Run)args.Node).Text;
}
//else if(args.Node.NodeType==NodeType.Table)
//{
// Aspose.Words.Tables.Table table = args.Node as Aspose.Words.Tables.Table;
// table.AutoFit(AutoFitBehavior.AutoFitToWindow);
//}
}
/// <summary>
/// 创建Word对象
/// </summary>
/// <param name="isTemplate">true-模板导出,false-不是模板导出</param>
/// <param name="templatePath">模板路劲</param>
/// <returns></returns>
private static XWPFDocument CreateXWPFDocument(bool isTemplate = false, string templatePath = "")
void INodeChangingCallback.NodeInserting(NodeChangingArgs args)
{
XWPFDocument doc = null;
if (isTemplate && !string.IsNullOrEmpty(templatePath.Trim()))
{
using FileStream file = new FileStream(templatePath, FileMode.Open, FileAccess.Read);
//将文件流中模板加载到工作簿对象中
doc = new XWPFDocument(file);
}
else
{
doc = new XWPFDocument();
}
return doc;
}
/// <summary>
/// 返回文件流
/// </summary>
/// <param name="doc">文档对象</param>
/// <returns></returns>
private static byte[] GetStream(XWPFDocument doc)
void INodeChangingCallback.NodeRemoved(NodeChangingArgs args)
{
using MemoryStream ms = new MemoryStream();
doc.Write(ms);
ms.Flush();
ms.Position = 0;
return ms.ToArray();
}
}
/// <summary>
/// Word内容
/// </summary>
public class WordData
{
/// <summary>
/// Word行
/// </summary>
public List<WordColumn> WordRow { get; set; }
}
/// <summary>
/// Word列
/// </summary>
public class WordColumn
{
/// <summary>
/// 每一个单元格的值
/// </summary>
public string CellValue { get; set; }
void INodeChangingCallback.NodeRemoving(NodeChangingArgs args)
{
}
}
}
\ No newline at end of file
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/aspnet:3.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:3.0 AS build
WORKDIR /src
COPY ["Edu.WebApi/Edu.WebApi.csproj", "Edu.WebApi/"]
COPY ["Edu.Module.EduTask/Edu.Module.EduTask.csproj", "Edu.Module.EduTask/"]
COPY ["Edu.Repository/Edu.Repository.csproj", "Edu.Repository/"]
COPY ["Edu.Common/Edu.Common.csproj", "Edu.Common/"]
COPY ["Edu.Model/Edu.Model.csproj", "Edu.Model/"]
COPY ["Edu.Aop/Edu.Aop.csproj", "Edu.Aop/"]
COPY ["Edu.Cache/Edu.Cache.csproj", "Edu.Cache/"]
COPY ["Edu.Module.User/Edu.Module.User.csproj", "Edu.Module.User/"]
COPY ["Edu.ThirdCore/Edu.ThirdCore.csproj", "Edu.ThirdCore/"]
COPY ["Edu.Module.Log/Edu.Module.Log.csproj", "Edu.Module.Log/"]
COPY ["Edu.Module.Course/Edu.Module.Course.csproj", "Edu.Module.Course/"]
COPY ["Edu.Module.Goods/Edu.Module.Goods.csproj", "Edu.Module.Goods/"]
COPY ["Edu.Module.Public/Edu.Module.Public.csproj", "Edu.Module.Public/"]
COPY ["Edu.Module.Exam/Edu.Module.Exam.csproj", "Edu.Module.Exam/"]
COPY ["Edu.Module.Question/Edu.Module.Question.csproj", "Edu.Module.Question/"]
COPY ["Edu.Module.Web/Edu.Module.Web.csproj", "Edu.Module.Web/"]
COPY ["Edu.Module.QYWeChat/Edu.Module.QYWeChat.csproj", "Edu.Module.QYWeChat/"]
COPY ["Edu.Module.StudyAbroad/Edu.Module.StudyAbroad.csproj", "Edu.Module.StudyAbroad/"]
COPY ["Edu.Module.Customer/Edu.Module.Customer.csproj", "Edu.Module.Customer/"]
COPY ["Edu.Module.System/Edu.Module.System.csproj", "Edu.Module.System/"]
COPY ["Edu.Module.OKR/Edu.Module.OKR.csproj", "Edu.Module.OKR/"]
COPY ["Edu.Module.Finance/Edu.Module.Finance.csproj", "Edu.Module.Finance/"]
COPY ["Edu.Module.Duty/Edu.Module.Duty.csproj", "Edu.Module.Duty/"]
COPY ["Edu.Module.Advertising/Edu.Module.Advertising.csproj", "Edu.Module.Advertising/"]
RUN dotnet restore "Edu.WebApi/Edu.WebApi.csproj"
COPY . .
WORKDIR "/src/Edu.WebApi"
RUN dotnet build "Edu.WebApi.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Edu.WebApi.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Edu.WebApi.dll"]
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment