如何通过 OpenXML sdk 将 excel 2007 文档方向更改为横向

发布于 2024-09-08 22:31:08 字数 180 浏览 7 评论 0原文

我需要帮助将 Excel 2007 文档方向更改为横向。我没有找到任何关于此的有用信息。我为此使用 OpenXML SDK。 我唯一发现的是:当我创建一个新的工作表时,我应该设置 PageSetup() { Orientation = OrientationValue.Landscape};但这没有帮助。 有人可以帮助解决这个问题吗? 谢谢。

I need help with changing excel 2007 document orientation to landscape. I have not found any helpful information about this. I am using OpenXML SDK for this.
The only thing I have found: when I create a new Worksheet I should set PageSetup() { Orientation = OrientationValue.Landscape}; But this doesn't help.
Can anybody help with this problem?
Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情话墙 2024-09-15 22:31:08

OrientationValue.Landscape 的方向是正确的。您只需循环遍历所有工作表并在 PageSetup 元素上设置方向属性,即可将所有工作表设置为横向:

    public static void SetLandscape(SpreadsheetDocument document)
        {
            WorkbookPart workbookPart = document.WorkbookPart;
            IEnumerable<string> worksheetIds = workbookPart.Workbook.Descendants<Sheet>().Select(w => w.Id.Value);
            WorksheetPart worksheetPart;
            foreach (string worksheetId in worksheetIds)
            {
                worksheetPart = ((WorksheetPart)workbookPart.GetPartById(worksheetId));
                PageSetup pageSetup = worksheetPart.Worksheet.Descendants<PageSetup>().FirstOrDefault();
                if (pageSetup != null) 
                {
                     pageSetup.Orientation = OrientationValues.Landscape;
                }
                worksheetPart.Worksheet.Save();
            }
            workbookPart.Workbook.Save();
        }

我用来操作文档的模式是首先打开 Excel 并创建一个空白文档并保存它。然后,我使用我的代码打开该文档并执行我需要的任何工作。这样我就不必费心创建元素并确保事物位于正确的位置。我用来实现此目的的代码在这里:

public byte[] Export(string pathToExcelFile)
    {
        // Open the file from the drive
        byte[] byteArray = File.ReadAllBytes(pathToExcelFile)
        using (MemoryStream stream = new MemoryStream())
        {
            stream.Write(byteArray, 0, (int)byteArray.Length);
            using (SpreadsheetDocument spreadsheetDoc = SpreadsheetDocument.Open(stream, true))
            {
                // Do all work on excel doc here
                SetLandscape(spreadsheetDoc);

                // Save all the changes
            }

            return stream.ToArray();
        }
    }

所以在这里我将文件从驱动器打开到内存流中,以便我可以在内存中执行所有编辑。然后,我在 SetLandscape 方法中传递该文档,它将在所有三张纸(3 张纸,因为这是空白 excel 2007 文档的默认值)上设置横向属性。然后,我保存更改并将流作为字节数组返回。我这样做是为了可以下载该文件。我建议您创建一个空白文件并将其打开到内存中,如下所示,而不是手动尝试从头开始构建该文件。这可以解释为什么你会得到这么多空指针。

You're on the right track with the OrientationValue.Landscape. You just need to loop through all worksheets and set the orientation attribute on the PageSetup element in order to set all worksheets to landscape:

    public static void SetLandscape(SpreadsheetDocument document)
        {
            WorkbookPart workbookPart = document.WorkbookPart;
            IEnumerable<string> worksheetIds = workbookPart.Workbook.Descendants<Sheet>().Select(w => w.Id.Value);
            WorksheetPart worksheetPart;
            foreach (string worksheetId in worksheetIds)
            {
                worksheetPart = ((WorksheetPart)workbookPart.GetPartById(worksheetId));
                PageSetup pageSetup = worksheetPart.Worksheet.Descendants<PageSetup>().FirstOrDefault();
                if (pageSetup != null) 
                {
                     pageSetup.Orientation = OrientationValues.Landscape;
                }
                worksheetPart.Worksheet.Save();
            }
            workbookPart.Workbook.Save();
        }

The pattern I use to manipulate documents is to first open excel and create a blank document and save it. I then use my code to open that document and doing any work I need to it. This way I don't have to be bothered with creating a the elements and making sure things are in the right place. The code I use to achieve this is here:

public byte[] Export(string pathToExcelFile)
    {
        // Open the file from the drive
        byte[] byteArray = File.ReadAllBytes(pathToExcelFile)
        using (MemoryStream stream = new MemoryStream())
        {
            stream.Write(byteArray, 0, (int)byteArray.Length);
            using (SpreadsheetDocument spreadsheetDoc = SpreadsheetDocument.Open(stream, true))
            {
                // Do all work on excel doc here
                SetLandscape(spreadsheetDoc);

                // Save all the changes
            }

            return stream.ToArray();
        }
    }

So here I open the file from the drive into a memory stream so I can perform all the edits in memory. I then pass that document in the SetLandscape method and it will set the landscape property on all three sheets (3 sheets since that is the default for a blank excel 2007 document). I then save my changes and return the stream as a byte array. I do this so the file can be downloaded. I recommend that you create a blank file and open it into memory like this instead of manually trying to build up the file from scratch. That would explain why you are getting so many null pointers.

一场春暖 2024-09-15 22:31:08

我用以下方法解决:

PageSetup pageSetup = worksheetPart.Worksheet.Descendants<PageSetup>().FirstOrDefault();
if (pageSetup == null)
{
    pageSetup = new PageSetup();
    pageSetup.Orientation = OrientationValues.Landscape;
    worksheetPart.Worksheet.AppendChild(pageSetup);
}

I solve with:

PageSetup pageSetup = worksheetPart.Worksheet.Descendants<PageSetup>().FirstOrDefault();
if (pageSetup == null)
{
    pageSetup = new PageSetup();
    pageSetup.Orientation = OrientationValues.Landscape;
    worksheetPart.Worksheet.AppendChild(pageSetup);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文