在 Sheetdata 之前追加列

Append columns before Sheetdata

我需要在创建新的 Excel 文件时向工作表添加具有特定宽度的列。根据我看到的所有地方,这必须在 Sheetdata . However I tried numerious things but can't get It working. My code for creating Excel file is from official site (link here - It's meant for ASP.NET but works fine for me).

之前完成

这是我的代码和最后一次让它工作的尝试:

 public void Export_Datagridview(DataGridView dgv, string filename)
 {
            using (var workbook = SpreadsheetDocument.Create(filename, SpreadsheetDocumentType.Workbook))
            {
                var workbookPart = workbook.AddWorkbookPart();

                workbook.WorkbookPart.Workbook = new Workbook();
                workbook.WorkbookPart.Workbook.Sheets = new Sheets();

                var sheetPart = workbook.WorkbookPart.AddNewPart<WorksheetPart>();

                var sheetData = new SheetData();
                //this part is giving me "object reference" error
                sheetPart.Worksheet.InsertBefore(AutoFit_Columns(dgv, sheetPart.Worksheet), sheetData);

  ...//and so on...

以及添加列的代码:

 private Columns AutoFit_Columns(DataGridView dgv, Worksheet worksheet)
        {
            Columns cols = new Columns();

            for (int col = 0; col < dgv.ColumnCount; colc++)
            {
                double max_width = 14.5; //something like default width in Excel

                for (int row = 0; row < dgv.RowCount; row++)
                {
                    double cell_width = Text_width(dgv.Rows[row].Cells[col].Value.ToString(), new System.Drawing.Font("Arial", 12.0F));

                    if (cell_width > max_width)
                    {
                        max_width = cell_width;
                    }

                    if (row == dgv.RowCount - 1) //last iteration - here we allready have max width within column
                    {
                       Column c = new Column() { Min = Convert.ToUInt32(col), Max = Convert.ToUInt32(col), Width = max_width, CustomWidth = true };
                        cols.Append(c);
                        worksheet.Append(cols);
                    }
                }
            }
            return cols;

        }

如您所见,这是我尝试根据从 Datagridview 导出的数据自动调整列。但在我可以测试其他代码之前,我需要正确地添加列。感谢您的帮助!

想通了,解决和识别所有问题并不是那么容易。首先,我需要将上面的代码更改为:

 public void Export_Datagridview(DataGridView dgv, string filename)
 {
            using (var workbook = SpreadsheetDocument.Create(filename, SpreadsheetDocumentType.Workbook))
            {
                var workbookPart = workbook.AddWorkbookPart();

                workbook.WorkbookPart.Workbook = new Workbook();
                workbook.WorkbookPart.Workbook.Sheets = new Sheets();

                var sheetPart = workbook.WorkbookPart.AddNewPart<WorksheetPart>();
                var sheetData = new SheetData();
                //this part is new - I had to change method for autofit too
                 sheetPart.Worksheet = new Worksheet();
                sheetPart.Worksheet.Append(AutoFit_Columns(dgv));
                sheetPart.Worksheet.Append(sheetData);
  ...//and so on...

然后更改自动调整方法。它仍然导致错误,因为循环从 0 开始,并且没有索引为 0 的列。将其更改为 1:

 for (int col = 1; col < dgv.ColumnCount; colc++)
            {

Excel 现在打开时设置了列宽,但自动调整代码需要调整。如果有人感兴趣, 是我包含自动调整的完整解决方案。