winforms 将DataGridView行和列导出到Excel并设置格式

jfgube3f  于 2023-06-24  发布在  其他
关注(0)|答案(1)|浏览(103)

我刚刚使我的数据库databounddatagridview符合我的要求,我想将它导出到excel。但是,我想只导出可见的行和列,并保留格式。我还覆盖了paint方法来“合并”一些列和/或行。有人知道从哪里开始吗?
我还覆盖了paint方法来“合并”一些列和/或行。有人知道从哪里开始吗?

**编辑:**到目前为止,我已经设法想出了这个,这是从另一个来源我不记得的代码:

private void button1_Click(object sender, EventArgs e)
        {
            string fileName;

            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "xls files (*.xlsx)|*.xlsx|All files (*.*)|*.*";
            saveFileDialog1.Title = "To Excel";
            saveFileDialog1.FileName = "Contrôle journalier du " + DateTime.Today.ToString("yyyy-MM-dd") + " de " + comboBox1.Text + comboBox2.Text;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                fileName = saveFileDialog1.FileName;
                var workbook = new XLWorkbook();
                var worksheet = workbook.Worksheets.Add(this.Text);
                for (int i = 0; i < dataGridView2.Columns.Count; i++)
                {
                    if (dataGridView2.Columns[i].Visible == true)
                    {
                        worksheet.Cell(11, i + 1).Value = dataGridView2.Columns[i].HeaderText;
                    }
                }

                for (int i = 0; i < dataGridView2.Rows.Count; i++)
                {
                    for (int j = 0; j < dataGridView2.Columns.Count; j++)
                    {
                        if ((dataGridView2.Rows[i].Visible == true) && (dataGridView2.Columns[j].Visible == true))
                        {
                            worksheet.Cell(i + 14, j + 1).Value = dataGridView2.Rows[i].Cells[j].Value?.ToString();
                        }
                        if (worksheet.Cell(i + 14, j + 1).Value.ToString().Length > 0)
                        {
                            XLAlignmentHorizontalValues align;

                            switch (dataGridView2.Rows[i].Cells[j].Style.Alignment)
                            {
                                case DataGridViewContentAlignment.BottomRight:
                                    align = XLAlignmentHorizontalValues.Right;
                                    break;
                                case DataGridViewContentAlignment.MiddleRight:
                                    align = XLAlignmentHorizontalValues.Right;
                                    break;
                                case DataGridViewContentAlignment.TopRight:
                                    align = XLAlignmentHorizontalValues.Right;
                                    break;

                                case DataGridViewContentAlignment.BottomCenter:
                                    align = XLAlignmentHorizontalValues.Center;
                                    break;
                                case DataGridViewContentAlignment.MiddleCenter:
                                    align = XLAlignmentHorizontalValues.Center;
                                    break;
                                case DataGridViewContentAlignment.TopCenter:
                                    align = XLAlignmentHorizontalValues.Center;
                                    break;

                                default:
                                    align = XLAlignmentHorizontalValues.Left;
                                    break;
                            }

                            
                        }
                    }
                }

                worksheet.Columns().AdjustToContents();
                workbook.SaveAs(fileName);
            }
        }

接下来,我将弄清楚如何删除所有空行,我认为这并不难。

编辑2:

我在工作簿后面添加了几行。SaveAs(fileName)行:

Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook(fileName);

                // Get the worksheets collection in the spreadsheet.
WorksheetCollection sheets = wb.Worksheets;

                // Get first Worksheet from WorksheetCollection by index.
Aspose.Cells.Worksheet sheet = sheets[0];
                // Or by name.
                // Worksheet sheet = sheets["Sheet1"];

                // Delete the Blank Rows from the worksheet.
sheet.Cells.DeleteBlankRows();

                // Save the excel file.
wb.Save(fileName);
0mkxixxg

0mkxixxg1#

如果你想跳过行,你需要另一个变量来表示行的值。类似下面的代码

int row = 0;
for (int i = 0; i < dataGridView2.Rows.Count; i++)
 {
     for (int j = 0; j < dataGridView2.Columns.Count; j++)
     {
         if ((dataGridView2.Rows[i].Visible == true) && (dataGridView2.Columns[j].Visible == true))
         {
             worksheet.Cell(row + 14, j + 1).Value = dataGridView2.Rows[i].Cells[j].Value?.ToString();

             if (worksheet.Cell(row + 14, j + 1).Value.ToString().Length > 0)
             {
                 XLAlignmentHorizontalValues align;

                 switch (dataGridView2.Rows[i].Cells[j].Style.Alignment)
                 {
                     case DataGridViewContentAlignment.BottomRight:
                           align = XLAlignmentHorizontalValues.Right;
                           break;
                     case DataGridViewContentAlignment.MiddleRight:
                       align = XLAlignmentHorizontalValues.Right;
                       break;
                     case DataGridViewContentAlignment.TopRight:
                       align = XLAlignmentHorizontalValues.Right;
                       break;

                     case DataGridViewContentAlignment.BottomCenter:
                       align = XLAlignmentHorizontalValues.Center;
                       break;
                     case DataGridViewContentAlignment.MiddleCenter:
                       align = XLAlignmentHorizontalValues.Center;
                       break;
                     case DataGridViewContentAlignment.TopCenter:
                       align = XLAlignmentHorizontalValues.Center;
                       break;

                     default:
                       align = XLAlignmentHorizontalValues.Left;
                       break;
             }

             row++;
         }
     }
 }

相关问题