OPENXML添加新行现有的Excel文件 [英] OpenXML add new row to existing Excel file

查看:354
本文介绍了OPENXML添加新行现有的Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多的XLSX文件,我需要在最后一个文件中添加一个新行。我使用的OpenXML,到目前为止,我知道如何打开/创建小号preadsheet,但我的搜索添加新行现有的文件返回任何内容。任何想法?

I've got lots of XLSX files and I need to append a new row after the last one in the file. I'm using OpenXML and so far I know how to open/create spreadsheet, but my search for adding new rows to existing files returned nothing. Any ideas ?

推荐答案

如果你需要做的是一个空行添加到末尾,如果已存在某行的行索引,在你不在乎,那么以下应为你工作:

If all you need to do is add a blank row to the end and you don't care if a row already exists at the row index, then the following should work for you:

    public static void InsertRow(WorksheetPart worksheetPart)
    {
        SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();  
        Row lastRow = sheetData.Elements<Row>().LastOrDefault();

        if (lastRow != null)
        {
            sheetData.InsertAfter(new Row() { RowIndex = (lastRow.RowIndex + 1) }, lastRow); 
        }
        else
        {
            sheetData.Insert(new Row() { RowIndex = 0 });
        }
    }

这篇关于OPENXML添加新行现有的Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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