我使用Word和OpenXml在C#ASP.NET Web应用程序中提供邮件合并function: 1)上传文档,其中包含许多预定义的字符串以进行替换。 2)使用OpenXML SDK 2.0我打开Word文档,将mainDocumentPart作为字符串并使用Regex执行替换。 3)然后我使用OpenXML创建一个新文档,添加一个新的mainDocumentPart并将替换产生的字符串插入到这个mainDocumentPart中。 但是,新文档中的所有格式/样式等都将丢失。 我猜我可以单独复制和添加样式,定义,注释部分等来模仿原始文档。 但是有没有一种方法使用Open XML复制文档,允许我在新副本上执行替换? 谢谢。
我正在创建一个示例处理程序来生成简单的Word文档。 本文档将包含文本Hello world 这是我使用的代码(C#.NET 3.5), 我创建了Word文档,但没有文本,大小为0。 我该如何解决? (我使用CopyStream方法,因为CopyTo仅在.NET 4.0及更高版本中可用。) public class HandlerCreateDocx : IHttpHandler { public void ProcessRequest(HttpContext context) { using (MemoryStream mem = new MemoryStream()) { // Create Document using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(mem, WordprocessingDocumentType.Document, true)) { // Add a main document part. MainDocumentPart mainPart = wordDocument.AddMainDocumentPart(); // Create the document structure and add […]
我在其中一个单元格的xlsx文件中有一个格式为“4/5/2011”(月/日/年)的日期。 我试图解析文件并在某些类中加载这些数据。 到目前为止,我解析单元格的部分如下所示: string cellValue = cell.InnerText; if (cell.DataType != null) { switch (cell.DataType.Value) { case CellValues.SharedString: // get string from shared string table cellValue = this.GetStringFromSharedStringTable(int.Parse(cellValue)); break; } } 我希望date可以是cell.DataType。 事实是,在使用日期“4/5/2011”解析单元格时,cell.DataType的值为null,单元格的值为“40638”,并且它不是共享字符串表的索引。 (我之前尝试过,结果是exception。) 有任何想法吗? 谢谢
使用OpenXML,我可以按页码阅读文档内容吗? wordDocument.MainDocumentPart.Document.Body提供完整文档的内容。 public void OpenWordprocessingDocumentReadonly() { string filepath = @”C:\…\test.docx”; // Open a WordprocessingDocument based on a filepath. using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(filepath, false)) { // Assign a reference to the existing document body. Body body = wordDocument.MainDocumentPart.Document.Body; int pageCount = 0; if (wordDocument.ExtendedFilePropertiesPart.Properties.Pages.Text != null) { pageCount = Convert.ToInt32(wordDocument.ExtendedFilePropertiesPart.Properties.Pages.Text); } for (int i = […]
我正在尝试使用C#替换整个 DOCX文件上的特定字符串,并使用换行符(换行符)。 我要搜索的文本字符串可以位于文件的段落或表格中。 我目前正在使用下面的代码替换文本。 using (WordprocessingDocument doc = WordprocessingDocument.Open(“yourdoc.docx”, true)) { var body = doc.MainDocumentPart.Document.Body; foreach (var text in body.Descendants()) { if (text.Text.Contains(“##Text1##”)) { text.Text = text.Text.Replace(“##Text1##”, Environment.NewLine); } } } 问题:当我运行此代码时,输出DOCX文件将文本替换为空格(即“”)而不是换行符。 如何更改此代码才能使其正常工作?
我正在尝试使用OpenXML以xlsx格式创建Excel文件,因为我需要在Web服务器上使用它。 填写表格中的值没有任何问题; 但是我很难在单元格中设置经典的日期格式。 下面是使用DocumentFormat.OpenXml和WindowsBase引用的快速测试。 class Program { static void Main(string[] args) { BuildExel(@”C:\test.xlsx”); } public static void BuildExel(string fileName) { using (SpreadsheetDocument myWorkbook = SpreadsheetDocument.Create(fileName, SpreadsheetDocumentType.Workbook)) { // Workbook Part WorkbookPart workbookPart = myWorkbook.AddWorkbookPart(); var worksheetPart = workbookPart.AddNewPart(); string relId = workbookPart.GetIdOfPart(worksheetPart); // File Version var fileVersion = new FileVersion { ApplicationName = “Microsoft Office […]
我需要读取XLSX文件并从中提取最大量的内容。 我应该使用哪个API? OLE DB,打开XML SDK或Excel Interop? 哪个最容易使用? 你能用一个或另一个检索所有信息吗? 即日期,时间,合并单元格,表格,数据透视表等
我正在将HTML文件导出到Open XML wordfile。 如果在HTML 中使用,我想在该部分添加Heading1样式。 但不知何故,当我在Microsoft Word 2010中打开文档时,未应用样式。 如果我在Libre Office中打开创建的文档,则会应用一些样式。 我自己也定义了一些样式,如果我使用其中一种样式,Word和Libre Office中的一切都很顺利。 我打开了适用于Microsoft Office的Open XML SDK 2.5生产力工具 ,当我查看它提供的示例代码时,它建议: ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId(){ Val = “Kop1” }; Kop1(而不是Heading1)是因为我的Word是荷兰语所以我的模板是荷兰语,但这并没有解决问题。 在此代码示例中,我创建了段落并向其添加样式和文本: using (wordDocument = WordprocessingDocument.Open(documentStream, true)) { MainDocumentPart mainPart = wordDocument.MainDocumentPart; WP.Body body = wordDocument.MainDocumentPart.Document.Body; WP.Paragraph para = body.AppendChild(new WP.Paragraph()); StyleDefinitionsPart part = wordDocument.MainDocumentPart.StyleDefinitionsPart; if (part != […]
我需要以编程方式使用C#将几个预先存在的docx文件附加到一个长的docx文件中 – 包括子弹和图像等特殊标记。 页眉和页脚信息将被删除,因此这些信息不会导致任何问题。 我可以找到有关使用.NET Framework 3操作单个docx文件的大量信息,但是关于如何合并文件并不容易或明显。 还有一个第三方程序(Acronis.Words)可以做到这一点,但它非常昂贵。 更新: 已经建议通过Word进行自动化,但是我的代码将在IIS Web服务器上的ASP.NET上运行,因此对我而言,不能选择使用Word。 很抱歉没有提到这一点。
我正在尝试使用Interop.Word.Application打开.docx文件并转换为PDF。 它作为控制台应用程序,但如果我在我的Web应用程序中使用相同的,它不起作用。 我试图查看该文件夹的权限。 我给了’网络服务’完全控制,但我仍然没有设置对象引用word.Documents.Open。 你能告诉我可能是什么问题吗? 我遇到了这个错误。 请告诉我。 我很感激任何建议。 谢谢。 Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application(); object oMissing = System.Reflection.Missing.Value; word.Visible = false; word.ScreenUpdating = false; string fileName = @”c:\OUTPUT\test.docx”); Document doc = word.Documents.Open(filename, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref […]