如何通过OpenXML SDK将Excel 2007文档的方向更改为横向 [英] How change excel 2007 document orientation to landscape by OpenXML sdk

查看:170
本文介绍了如何通过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.

推荐答案

您在OrientationValue.Landscape的正确轨道上.您只需要遍历所有工作表并在PageSetup元素上设置direction属性,即可将所有工作表设置为横向:

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();
        }

我用来处理文档的模式是首先打开excel并创建一个空白文档并保存.然后,我使用代码打开该文档并对其进行所需的任何工作.这样,我不必费心创建元素并确保事物放置在正确的位置.我用来实现此目的的代码在这里:

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();
        }
    }

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

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.

这篇关于如何通过OpenXML SDK将Excel 2007文档的方向更改为横向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆