【问题标题】:Programmatically pad a row in Excel in OpenXML以编程方式在 OpenXML 中的 Excel 中填充一行
【发布时间】:2019-07-08 18:43:44
【问题描述】:

我需要通过 OpenXML 在 Excel 行中添加一些额外的单元格。但是,下面的代码似乎并没有起到作用。希望有任何帮助以使这项工作正常进行。

private Row PadRow(Row row, int p)
{
    try
    {
        int currentcount = p - row.Descendants<Cell>().Count<Cell>();
        for (int count = 0; count < currentcount; count++)
            row.Append(new Cell() { DataType = CellValues.String, CellValue = new CellValue(" ") });
    }
    catch (Exception ex)
    {
        throw ex;
    };
    return row;
}

【问题讨论】:

    标签: c# excel openxml


    【解决方案1】:

    找到了一个很好的解决方法。

    http://lateral8.com/articles/2010/3/5/openxml-sdk-20-export-a-datatable-to-excel.aspx

          /// <summary>
            /// Gets the Excel column name based on a supplied index number.
            /// </summary>
            /// <returns>1 = A, 2 = B... 27 = AA, etc.</returns>
            private string getColumnName(int columnIndex)
            {
                int dividend = columnIndex;
                string columnName = String.Empty;
                int modifier;
    
                while (dividend > 0)
                {
                    modifier = (dividend - 1) % 26;
                    columnName =
                        Convert.ToChar(65 + modifier).ToString() + columnName;
                    dividend = (int)((dividend - modifier) / 26);
                }
    
                return columnName;
            }
            private Cell createTextCell(int columnIndex, int rowIndex,  object cellValue)
            {
                Cell cell = new Cell();
    
                cell.DataType = CellValues.InlineString;
                cell.CellReference = getColumnName(columnIndex) + rowIndex;
    
                InlineString inlineString = new InlineString();
                Text t = new Text();
    
                t.Text = cellValue.ToString();
                inlineString.AppendChild(t);
                cell.AppendChild(inlineString);
    
                return cell;
            }
    
            public void ProcessDCTRows(IEnumerable<Row> dataRows, SharedStringTable sharedString)
            {
                try
                {
                    //Extract the information for each row 
                    foreach (Row row in dataRows)
                    {
                        IEnumerable<String> cellValues;
                        if (row.Descendants<Cell>().Count<Cell>() < 234)
                        {
                            int lastcolindex = row.Descendants<Cell>().Count<Cell>(); 
                            int rowindex = Convert.ToInt32(row.RowIndex.ToString());
                            int currentcount = 234 ;
                            for (; lastcolindex < currentcount; lastcolindex++)
                            {
                                row.AppendChild( createTextCell(lastcolindex,rowindex," " ));
                            }
    
                        }
    
    .   . //add processing code here
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-13
      相关资源
      最近更新 更多