如何通过 OpenXML sdk 将 excel 2007 文档方向更改为横向
我需要帮助将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
OrientationValue.Landscape 的方向是正确的。您只需循环遍历所有工作表并在 PageSetup 元素上设置方向属性,即可将所有工作表设置为横向:
我用来操作文档的模式是首先打开 Excel 并创建一个空白文档并保存它。然后,我使用我的代码打开该文档并执行我需要的任何工作。这样我就不必费心创建元素并确保事物位于正确的位置。我用来实现此目的的代码在这里:
所以在这里我将文件从驱动器打开到内存流中,以便我可以在内存中执行所有编辑。然后,我在 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:
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:
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.
我用以下方法解决:
I solve with: