From 3a3d128ab077ecb08da0f4905a6b3a53ce6cadde Mon Sep 17 00:00:00 2001 From: callmeyan Date: Tue, 31 Dec 2024 19:41:30 +0800 Subject: [PATCH 1/2] update export style --- AIProofread/core/DocumentUtil.cs | 41 ++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/AIProofread/core/DocumentUtil.cs b/AIProofread/core/DocumentUtil.cs index 1f2f973..6914fc4 100644 --- a/AIProofread/core/DocumentUtil.cs +++ b/AIProofread/core/DocumentUtil.cs @@ -19,6 +19,7 @@ using System.Linq; using NPOI.SS.Formula.Functions; using AIProofread.Model; using System.Xml.Linq; +using NPOI.SS.Util; namespace AIProofread { @@ -593,6 +594,7 @@ namespace AIProofread simHeiFont.Color = NPOI.HSSF.Util.HSSFColor.Black.Index; // 设置表格样式 var style = CreateBaseCellStyle(book); + style.FillPattern = FillPattern.NoFill; // SolidForeground /* 系统与正文用不同字体区分(包括顶部栏、处理列、新增、删除等提示)勘误表系统字体为黑体,所有字号统一 */ style.SetFont(simHeiFont); style.BorderBottom = NPOI.SS.UserModel.BorderStyle.None; @@ -620,14 +622,43 @@ namespace AIProofread CreateCell(row, 5, simHeiFont, "建议"); CreateCell(row, 6, simHeiFont, "处理状态"); + + // 设置表头筛选及冻结 + sheet.CreateFreezePane(0, 1,0,1); + sheet.SetAutoFilter(CellRangeAddress.ValueOf("A1:G1")); + + // 设置表头背景颜色为黑色 + var headerStyle = CreateBaseCellStyle(book); + var headerFont = CreateBaseFont(book, "黑体", 11); + + headerFont.Color = NPOI.HSSF.Util.HSSFColor.White.Index; + headerStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Black.Index; + headerStyle.FillPattern = FillPattern.SolidForeground; + headerStyle.SetFont(headerFont); + + for (int i = 0; i < 7; i++) + { + var cell = row.Cells[i]; + cell.CellStyle = headerStyle; + //cell.CellStyle.FillPattern = NPOI.SS.UserModel.FillPattern.SolidForeground; + //// 设置背景 + //cell.CellStyle.FillPattern = NPOI.SS.UserModel.FillPattern.SolidForeground; + //cell.CellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Red.Index; + //cell.CellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Black.Index; + } + + + //sheet.AutoFilter = new NPOI.SS.Util.AutoFilter(0, 0, new NPOI.SS.Util.AreaReference("A1:G1")); + + // 设置宽度 + sheet.SetColumnWidth(0, 0); // 详细信息 sheet.SetColumnWidth(3, 80 * 256); // 详细信息 - sheet.SetColumnWidth(4, 15 * 256); // 异常 - sheet.SetColumnWidth(5, 15 * 256); // 建议 - sheet.SetColumnWidth(6, 8 * 256); // 建议 + sheet.SetColumnWidth(4, 20 * 256); // 异常 + sheet.SetColumnWidth(5, 20 * 256); // 建议 + sheet.SetColumnWidth(6, 10 * 256); // 处理状态 var blackFont = CreateBaseFont(book, NPOI.HSSF.Util.HSSFColor.Black.Index); - var redFont = CreateBaseFont(book, NPOI.HSSF.Util.HSSFColor.Red.Index); // 获取排序后的数据 @@ -780,7 +811,7 @@ namespace AIProofread // 保存到文件 book.Write(fs); //Globals.ThisAddIn.ShowMessage("导出成功", 3000); - Globals.ThisAddIn.ActiveDocument?.ShowMessage("导出勘误表成功", 3000, false); + Globals.ThisAddIn.ActiveDocument?.ShowMessage("导出勘误表成功", 2000, false); } } From 74262733201780d9f51236634300e04b5732bf52 Mon Sep 17 00:00:00 2001 From: callmeyan Date: Thu, 2 Jan 2025 16:07:28 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=AF=BC=E5=87=BA?= =?UTF-8?q?=E6=A0=B7=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AIProofread/Bridge.cs | 2 +- AIProofread/Model/DocumentInfo.cs | 8 ++- AIProofread/Model/ExportDataItem.cs | 26 ++++++- AIProofread/core/DocumentUtil.cs | 102 ++++++++++++++-------------- AIProofread/core/Tools.cs | 13 ++-- 5 files changed, 90 insertions(+), 61 deletions(-) diff --git a/AIProofread/Bridge.cs b/AIProofread/Bridge.cs index 91cc499..208abc7 100644 --- a/AIProofread/Bridge.cs +++ b/AIProofread/Bridge.cs @@ -314,7 +314,7 @@ namespace AIProofread } else { - Thread.Sleep(3000); + Thread.Sleep(500); //FormMessage loadingDialog = null; //// 大于15W字符无法校对 //Task.Run(() => diff --git a/AIProofread/Model/DocumentInfo.cs b/AIProofread/Model/DocumentInfo.cs index 93c97c2..d74858b 100644 --- a/AIProofread/Model/DocumentInfo.cs +++ b/AIProofread/Model/DocumentInfo.cs @@ -613,8 +613,14 @@ namespace AIProofread.Model if (marks.ContainsKey(item.Id)) continue; Logger.Log(string.Format("mark type {0} data {1}->{2}", item.Tag, item.Origin, item.Text)); int _prev = prevOffset; + bool isDisabled = false; + // 判断查找内容是否在原始数据中,否则直跳过 + if(item.Tag != "i" ) + { + isDisabled = correct.Insert.IndexOf(item.Origin) == -1; + } // 查找对应区域并再该区域添加书签 - var mark = DocumentUtil.FindRangeAndCreateBookmark(item, correct, CurrentDocument, ref prevOffset); + var mark = isDisabled ? null : DocumentUtil.FindRangeAndCreateBookmark(item, correct, CurrentDocument, ref prevOffset); // 防止调用方法中没有更新 if (_prev >= prevOffset) { diff --git a/AIProofread/Model/ExportDataItem.cs b/AIProofread/Model/ExportDataItem.cs index 3b58c26..235da1e 100644 --- a/AIProofread/Model/ExportDataItem.cs +++ b/AIProofread/Model/ExportDataItem.cs @@ -1,5 +1,6 @@ using Microsoft.Office.Interop.Word; using System.Collections.Generic; +using System.Text; using UtilLib; namespace AIProofread.Model @@ -11,6 +12,8 @@ namespace AIProofread.Model public string OriginSentence { get; set; } public CorrectItem Item { get; set; } + private static readonly int MAX_WORD_LENGTH = 5; + public ExportDataItem(CorrectItem item, int pageNumber, int lineNumber,string originSentence = "") { Item = item; @@ -19,6 +22,27 @@ namespace AIProofread.Model OriginSentence = originSentence; } + public static string GetSentence(ProofreadItem item) + { + var sentence = item.OriginSentence; + if (sentence.Length <= MAX_WORD_LENGTH) + { + return sentence; + } + var range = item.mark.Range; + + // 获取range所在句子 + //var first = range.Sentences.First.Start; + //var last = range.Sentences.Last.End; + var sentences = range.Sentences; + var sb = new StringBuilder(); + foreach (Microsoft.Office.Interop.Word.Range s in sentences) + { + sb.Append(s.Text); + } + return sb.ToString().Trim(); + } + public static List GetExportData(Dictionary marks) { List list = new List(); @@ -37,7 +61,7 @@ namespace AIProofread.Model var pageNumber = range.get_Information(WdInformation.wdActiveEndPageNumber); // 获取书签在当前页面的行数 var lineNumber = range.get_Information(WdInformation.wdFirstCharacterLineNumber); - list.Add(new ExportDataItem(it, pageNumber, lineNumber,item.Value.OriginSentence)); + list.Add(new ExportDataItem(it, pageNumber, lineNumber,GetSentence(item.Value))); } // 根据页码和行数排序 list.Sort((x, y) => diff --git a/AIProofread/core/DocumentUtil.cs b/AIProofread/core/DocumentUtil.cs index 6914fc4..c4304c3 100644 --- a/AIProofread/core/DocumentUtil.cs +++ b/AIProofread/core/DocumentUtil.cs @@ -20,6 +20,7 @@ using NPOI.SS.Formula.Functions; using AIProofread.Model; using System.Xml.Linq; using NPOI.SS.Util; +using NPOI.HSSF.Util; namespace AIProofread { @@ -560,14 +561,15 @@ namespace AIProofread } ProcessExport(sfd.FileName); } - catch (Exception) + catch (Exception e) { + Logger.Log(e); Globals.ThisAddIn.ShowDialog("导出勘误表失败,请重试", null, null); } } - private static NPOI.SS.UserModel.ICell CreateCell(IRow row, int colIndex, IFont font, string text) + private static NPOI.SS.UserModel.ICell CreateCell(IRow row, int colIndex, IFont font, string text,ICellStyle style = null) { var cell = row.CreateCell(colIndex); if (font != null) @@ -580,7 +582,15 @@ namespace AIProofread { cell.SetCellValue(text); } - cell.CellStyle.WrapText = true; + if(style != null) + { + cell.CellStyle = style; + + } + else + { + cell.CellStyle.WrapText = true; + } return cell; } @@ -603,61 +613,49 @@ namespace AIProofread style.BorderRight = NPOI.SS.UserModel.BorderStyle.None; style.WrapText = true; - sheet.SetDefaultColumnStyle(0, style); - sheet.SetDefaultColumnStyle(1, style); - sheet.SetDefaultColumnStyle(2, style); - sheet.SetDefaultColumnStyle(3, style); - sheet.SetDefaultColumnStyle(4, style); - sheet.SetDefaultColumnStyle(5, style); - sheet.SetDefaultColumnStyle(6, style); - - - // 表头设置 - var row = sheet.CreateRow(0); - CreateCell(row, 0, simHeiFont, "序号"); - CreateCell(row, 1, simHeiFont, "页"); - CreateCell(row, 2, simHeiFont, "行"); - CreateCell(row, 3, simHeiFont, "详细信息"); - CreateCell(row, 4, simHeiFont, "异常"); - CreateCell(row, 5, simHeiFont, "建议"); - CreateCell(row, 6, simHeiFont, "处理状态"); - - - // 设置表头筛选及冻结 - sheet.CreateFreezePane(0, 1,0,1); - sheet.SetAutoFilter(CellRangeAddress.ValueOf("A1:G1")); - - // 设置表头背景颜色为黑色 - var headerStyle = CreateBaseCellStyle(book); - var headerFont = CreateBaseFont(book, "黑体", 11); - - headerFont.Color = NPOI.HSSF.Util.HSSFColor.White.Index; - headerStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Black.Index; - headerStyle.FillPattern = FillPattern.SolidForeground; - headerStyle.SetFont(headerFont); - - for (int i = 0; i < 7; i++) - { - var cell = row.Cells[i]; - cell.CellStyle = headerStyle; - //cell.CellStyle.FillPattern = NPOI.SS.UserModel.FillPattern.SolidForeground; - //// 设置背景 - //cell.CellStyle.FillPattern = NPOI.SS.UserModel.FillPattern.SolidForeground; - //cell.CellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Red.Index; - //cell.CellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Black.Index; - } - - - //sheet.AutoFilter = new NPOI.SS.Util.AutoFilter(0, 0, new NPOI.SS.Util.AreaReference("A1:G1")); - - // 设置宽度 - sheet.SetColumnWidth(0, 0); // 详细信息 + sheet.SetColumnWidth(0, 1); // 序号 + sheet.SetColumnHidden(0, true); // 隐藏序号 sheet.SetColumnWidth(3, 80 * 256); // 详细信息 sheet.SetColumnWidth(4, 20 * 256); // 异常 sheet.SetColumnWidth(5, 20 * 256); // 建议 sheet.SetColumnWidth(6, 10 * 256); // 处理状态 + string[] headerTitles = + { + "序号","页","行","详细信息","异常", "建议","处理状态" + }; + + // 设置表头筛选及冻结 2.0.5 2024-12-30修改 + sheet.CreateFreezePane(0, 1,0,1); + sheet.SetAutoFilter(CellRangeAddress.ValueOf("A1:G1")); + + // 设置表头为白色 + var headerStyle = book.CreateCellStyle(); + var headerFont = book.CreateFont(); + headerFont.FontName = "黑体"; // 设置字体 + headerFont.FontHeightInPoints = 11; // 字体大小 + var themeColor = new XSSFColor(new byte[] { 214, 170, 105 }); // 首行背景色黑色,字体用主题金色 + ((XSSFFont)headerFont).SetColor(themeColor); + headerStyle.SetFont(headerFont); // 应用字体 + + // 设置背景颜色为黑色 + headerStyle.FillForegroundColor = HSSFColor.Black.Index; // 设置前景色 + headerStyle.FillPattern = FillPattern.SolidForeground; // 使用实心填充模式 + + // 对齐方式 + headerStyle.VerticalAlignment = VerticalAlignment.Center; // 垂直居中 + headerStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; // 水平居中 + + // 表头设置 + var row = sheet.CreateRow(0); + row.Height = 40 * 20; // 首行加高 + for (int i = 0; i < 7; i++) + { + sheet.SetDefaultColumnStyle(i, style); + var cell = CreateCell(row, i, headerFont, headerTitles[i], headerStyle); + } + var blackFont = CreateBaseFont(book, NPOI.HSSF.Util.HSSFColor.Black.Index); var redFont = CreateBaseFont(book, NPOI.HSSF.Util.HSSFColor.Red.Index); diff --git a/AIProofread/core/Tools.cs b/AIProofread/core/Tools.cs index 103b78f..71cd2f1 100644 --- a/AIProofread/core/Tools.cs +++ b/AIProofread/core/Tools.cs @@ -1,5 +1,6 @@ using Microsoft.Office.Interop.Word; using Newtonsoft.Json; +using NPOI.POIFS.FileSystem; using NPOI.XWPF.UserModel; using System; using System.Collections.Generic; @@ -23,11 +24,11 @@ namespace AIProofread { string ext = doc.FullName.ToLower(); // 如果是 - if (ext.EndsWith(".wps") || doc.Paragraphs.Count < 200 || doc.Tables.Count < 20) - { - // 如果段落数小于200或表格小于20 则直接使用vsto 获取数据 - return GetAllTextByVSTO(doc); - } + //if (ext.EndsWith(".wps") || doc.Paragraphs.Count < 200 || doc.Tables.Count < 20) + //{ + // // 如果段落数小于200或表格小于20 则直接使用vsto 获取数据 + // return GetAllTextByVSTO(doc); + //} // 创建临时文件 方便数据读取 string docPath = Tools.GetReadDocumentFilePath(doc); @@ -58,7 +59,7 @@ namespace AIProofread { foreach (var pc in cell.Paragraphs) { - //list.Add(pc.ParagraphText); + list.Add(new DocumentText(pc.ParagraphText.Replace("\u0002", ""), paragraphNumber)); paragraphNumber++; } }