Export the dataGridView to Excel with all the cells format

后端 未结 3 1549
我寻月下人不归
我寻月下人不归 2020-11-27 06:37

I have this code that I know that it works fast

CopyAlltoClipboard(dataGridViewControl);
Microsoft.Office.Interop.Excel.Application xlexcel;
Microsoft.Office         


        
3条回答
  •  鱼传尺愫
    2020-11-27 06:57

    The crux of your question is using the Clipboard on a DataGridView does not contain Cell Formatting. Because the Clipboard doesn't contain the formatting you're back to the original slow performance problem of having to set Cell Styles individually, which using Interop is very, very slow.

    In this case it will work better to create Excel files using XML instead of Interop. Below is a method using ClosedXML to export a DataGridView to Excel with formatting.

    using ClosedXML.Excel;
    
    public void ExportToExcelWithFormatting(DataGridView dataGridView1)
    {
        string fileName;
    
        SaveFileDialog saveFileDialog1 = new SaveFileDialog();
        saveFileDialog1.Filter = "xls files (*.xlsx)|*.xlsx|All files (*.*)|*.*";
        saveFileDialog1.Title = "To Excel";
        saveFileDialog1.FileName = this.Text + " (" + DateTime.Now.ToString("yyyy-MM-dd") + ")";
    
        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            fileName = saveFileDialog1.FileName;
            var workbook = new XLWorkbook();
            var worksheet = workbook.Worksheets.Add(this.Text);
            for (int i = 0; i < dataGridView1.Columns.Count; i++)
            {
                worksheet.Cell(1, i + 1).Value = dataGridView1.Columns[i].Name;
            }
    
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                for (int j = 0; j < dataGridView1.Columns.Count; j++)
                {
                    worksheet.Cell(i + 2, j + 1).Value = dataGridView1.Rows[i].Cells[j].Value.ToString();
    
                    if (worksheet.Cell(i + 2, j + 1).Value.ToString().Length > 0)
                    {
                        XLAlignmentHorizontalValues align;
    
                        switch (dataGridView1.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.Cell(i + 2, j + 1).Style.Alignment.Horizontal = align;
    
                        XLColor xlColor = XLColor.FromColor(dataGridView1.Rows[i].Cells[j].Style.SelectionBackColor);
                        worksheet.Cell(i + 2, j + 1).AddConditionalFormat().WhenLessThan(1).Fill.SetBackgroundColor(xlColor);
    
                        worksheet.Cell(i + 2, j + 1).Style.Font.FontName = dataGridView1.Font.Name;
                        worksheet.Cell(i + 2, j + 1).Style.Font.FontSize = dataGridView1.Font.Size;
    
                    }                                           
                }
            }
            worksheet.Columns().AdjustToContents();
            workbook.SaveAs(fileName);
            //MessageBox.Show("Done");
        }
    }
    

提交回复
热议问题