标签: openxml

ToOADate(); 转换儒略日期 Excel

我正在使用 OpenXML 读取和 Excel 电子表格,并将日期存储为数字。据我了解,这是一种朱利安格式,我在谷歌上搜索的每个地方都说要使用 ToOADate(); 将这些数字转换为日期。

我遇到的问题是 ToOADate(); 在我的编译中没有解决。我正在使用 Visual Basic 2013,C# Asp.net MVC 应用程序。

看来这是在系统命名空间中,但我已经有一个 using System 语句。

我显然错过了一些非常基本的东西 - 有人可以帮忙吗?

 var test = 41725;
DateTime test2 = test.ToOADate();
Run Code Online (Sandbox Code Playgroud)

我得到的编译错误是

错误 4“int”不包含“ToOADate”的定义,并且找不到接受“int”类型的第一个参数的扩展方法“ToOADate”(您是否缺少 using 指令或程序集引用?)

c# excel datetime ms-office openxml

1
推荐指数
1
解决办法
2145
查看次数

C# - OpenXML SDK 2.5 - 从幻灯片母版插入新幻灯片,布局包含图像

我在 OpenXML 2.5 SDK 的帮助下用它来创建我的新幻灯片。我设计并使用自己的幻灯片母版来创建一张新幻灯片。我的幻灯片母版包括一些带图像的布局和一些不带图像的布局。

如果我从主布局创建没有图像的幻灯片,则一切正常。如果我使用包含图像的布局创建一张幻灯片,我会得到正确的布局,但在每个固定图像之上还有另一个可移动图像与固定图像重叠,因此存在不必要的固定图像重复,我不需要在我新创建的幻灯片中。

我怎么解决这个问题?

我的代码如下:

       public static void InsertNewSlide(string presentationFile, int position, string layoutName)
      {
        using (PresentationDocument presentationDocument = PresentationDocument.Open(presentationFile, true))
        {

            InsertNewSlide(presentationDocument, position, layoutName);
        }
      }

    public static void InsertNewSlide(PresentationDocument presentationDocument, int position, string layoutName)
    {
        PresentationPart presentationPart = presentationDocument.PresentationPart;

        OpenXML.Slide slide = new OpenXML.Slide(new CommonSlideData(new ShapeTree()));

        SlidePart slidePart = presentationPart.AddNewPart<SlidePart>();

        slide.Save(slidePart);

        SlideMasterPart slideMasterPart = presentationPart.SlideMasterParts.First();

        SlideLayoutPart slideLayoutPart = slideMasterPart.SlideLayoutParts.SingleOrDefault(sl => sl.SlideLayout.CommonSlideData.Name.Value.Equals(layoutName, StringComparison.OrdinalIgnoreCase));

        slidePart.AddPart<SlideLayoutPart>(slideLayoutPart);

        slidePart.Slide.CommonSlideData = (CommonSlideData)slideMasterPart.SlideLayoutParts.SingleOrDefault(sl => sl.SlideLayout.CommonSlideData.Name.Value.Equals(layoutName)).SlideLayout.CommonSlideData.Clone();

        using (Stream stream = slideLayoutPart.GetStream()) …
Run Code Online (Sandbox Code Playgroud)

c# openxml openxml-sdk

1
推荐指数
1
解决办法
2308
查看次数

向 OpenXML 文档添加标头

我正在开发 asp.net webform,完成后用户需要下载一个 Word 文档来总结他们选择的答案。我已经成功地将 html 保存为 .doc,但现在客户想要 .docx。我已经能够成功生成他们可以下载的文档,但我一生都无法显示标题。

似乎有点不对劲的一件事是,如果我使用 Open SML 生产力工具,我在 word 中创建的文档将具有 /word/header1.xml 2 和 3 标签,我的文档将只有 /word/header.xml。但我不知道如何改变它。

这是我一直在使用的所有代码:

public static void CreateWordprocessingDocument(string filepath)
{
    // Create a document by supplying the filepath. 
    using (WordprocessingDocument wordDocument =
        WordprocessingDocument.Create(filepath, WordprocessingDocumentType.Document))
    {
        // Add a main document part. 
        MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
        AddSettingsToMainDocumentPart(mainPart);

        // Create the document structure and add some text.
        mainPart.Document = new Document();

        HeaderPart headerPart = mainPart.AddNewPart<HeaderPart>("rId7");
        GenerateHeader(headerPart);

        Body body = mainPart.Document.AppendChild(new Body());
        Paragraph para = …
Run Code Online (Sandbox Code Playgroud)

c# asp.net openxml

1
推荐指数
1
解决办法
5304
查看次数

C# OpenXML 将 Excel 工作表复制并粘贴到新工作簿

我有多个具有相同工作表名称的工作簿。我想将所有单独的工作表复制到一本工作簿中。到目前为止,我的代码复制了文本,但它不保持格式,所以我丢失了颜色、边框等。有人可以建议任何改进来包含样式并保持格式吗?谢谢

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using System.Linq;
namespace openxmlExcelTryout
{


class OpenXMLCopySheet
{
    private static int tableId;

    public static void CopySheet(string filename, string sheetName, string clonedSheetName, string destFileName)
    {

        //Open workbook
        using (SpreadsheetDocument mySpreadsheet = SpreadsheetDocument.Open(filename, true))
        {
            WorkbookPart workbookPart = mySpreadsheet.WorkbookPart;
            //Get the source sheet to be copied
            WorksheetPart sourceSheetPart = GetWorkSheetPart(workbookPart, sheetName);
            SharedStringTablePart sharedStringTable = workbookPart.SharedStringTablePart;
            //Take advantage of AddPart for deep cloning
            using (SpreadsheetDocument newXLFile = SpreadsheetDocument.Create(destFileName, SpreadsheetDocumentType.Workbook))
            {
                WorkbookPart newWorkbookPart = newXLFile.AddWorkbookPart();
                SharedStringTablePart newSharedStringTable …
Run Code Online (Sandbox Code Playgroud)

c# openxml

1
推荐指数
2
解决办法
1万
查看次数

禁用有关 OfficeOpenXml 中包含数值的 Excel 文本单元格的警告

我正在使用OfficeOpenXml创建 MS/Excel 电子表格文件。某些列包含任意文本值。但是,当这些列中的单元格填充了数值(即仅包含数字的文本值)时,Excel 会在这些单元格的角上显示一个小绿色三角形,并显示警告“此单元格中的数字格式为文本”或前面加撇号"

这在技术上是正确的,但我不希望 Excel 显示警告。

那么,如何将这些列或这些列中的单元格格式化为严格的文本值,以便它们不会被标记为数字文本值?如何禁用警告并强制 Excel 接受这些单元格值作为文本(仅)?

注意:我见过其他 OpenXML 包的解决方案,但没有专门针对 OfficeOpenXml 的解决方案。我还看到了将文本单元格值解释为数字的解决方案,但这与我想要做的完全相反。

excel openxml

1
推荐指数
1
解决办法
1628
查看次数

1
推荐指数
1
解决办法
1106
查看次数

有人有使用 NuGet 的 DocumentFormat.OpenXML.DotNet.Core 包构建控制台应用程序 (C#) 的经验吗?

我已在 Visual Studio 2019 中使用 C# 成功启动控制台应用程序解决方案,并从 NuGet 下载并安装了包 DocumentFormat.OpenXML.DotNet.Core。由于 DocumentFormat.OpenXML 不能与 .NET Core 一起使用,因此我无法使用它。我还成功地将包附加到解决方案,并且它显示在解决方案资源管理器中,没有任何警报。(我过去对其他软件包使用过相同的方法。)

这个包应该让我的解决方案能够访问 OpenXML 功能,但我无法让我的程序识别它。

我尝试为 DocumentFormat 插入 using 语句,但出现错误,指出 using 语句不是必需的 - 然后它询问我是否缺少 using 语句或汇编器引用。

我尝试将命名空间更改为 DocumentFormat,但随后我必须添加所有类和所有函数,在我看来,这就是该包的全部目的。

这是代码(它是示例代码,只是为了使其正常工作):

using System;
using DocumentFormat;
using S = DocumentFormat.OpenXml.Spreadsheet.Sheets;
using E = DocumentFormat.OpenXml.OpenXmlElement;
using A = DocumentFormat.OpenXml.OpenXmlAttribute;

namespace ConsoleApp1
{
    class Program
    {
        static void ReadExcelFile()
        {
            try
            {
                //Lets open the existing excel file and read through its content . Open the excel using openxml sdk
                using …
Run Code Online (Sandbox Code Playgroud)

c# openxml .net-core visual-studio-2019

1
推荐指数
1
解决办法
1531
查看次数

向Open Office XML添加间距和空行

我正在尝试使用Open Office XML输出word文件,但似乎无法获得正确的间距.

Variable name:ATTEND
 Description:Student's attendance status during the 
Run Code Online (Sandbox Code Playgroud)

我希望word文件是这样的(在:)之后有空格:

Variable name: ATTEND
Description:Student's attendance status during the 
Run Code Online (Sandbox Code Playgroud)

我的代码如下,间距消失:

start of my function
// Add a new main document part. 
            MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();

            // Create the Document DOM.
            mainPart.Document = new Document();

            Body body = mainPart.Document.AppendChild(new Body());

            ParagraphProperties paragraphProperties = new ParagraphProperties
            (
                new ParagraphStyleId() { Val = "No Spacing" },
                new SpacingBetweenLines() { After = "0" }
            );

            Paragraph para = body.AppendChild(new Paragraph(paragraphProperties));
            Run run = para.AppendChild(new …
Run Code Online (Sandbox Code Playgroud)

c# ms-word openxml openxml-sdk

0
推荐指数
1
解决办法
3373
查看次数

将TableStyle应用于Word表

尝试使用预定义的样式来样式化表,但是没有任何效果。我尝试了一个新创建的文档和一个从保存的模板创建的文档。使用SDK Productivity工具,我可以看到模板中有样式,但尚未应用。我尝试添加样式或直接设置样式,但似乎都没有用。

    public static void CreateWordprocessingDocument(string fileName) {

        string[,] data = {
            {"Texas", "TX"},
            {"California", "CA"},
            {"New York", "NY"},
            {"Massachusetts", "MA"}
        };

        using (var wordDocument = WordprocessingDocument.Open(fileName, true)) {

            // We need to change the file type from template to document.
            wordDocument.ChangeDocumentType(WordprocessingDocumentType.Document);

            var body = wordDocument.GetDocument().Body;

            Table table = new Table();

            TableProperties props = new TableProperties();
            TableStyle tableStyle = new TableStyle { Val = "Light Shading Accent 1" };
            props.TableStyle = tableStyle;
            //props.Append(tableStyle);
            table.AppendChild(props);

            for (var i = 0; i …
Run Code Online (Sandbox Code Playgroud)

c# ms-word docx openxml

0
推荐指数
1
解决办法
2679
查看次数

C#WordprocessingDocument-在单元格中插入图像

我有一种方法可以替换文档特定文本中的标签。如何更换标签图片? 在此处输入图片说明

c# image openxml wordprocessingml

0
推荐指数
1
解决办法
2276
查看次数