在C#中将Datagridview导出到Excel [英] Exporting Datagridview to Excel in C#

查看:138
本文介绍了在C#中将Datagridview导出到Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我更改了

  transcationTableDataGridView.Rows.Count  -  1 

to

  transcationTableDataGridView.Rows.Count + 1 

它会将所有行导出为ex​​cel,但抛出索引应为非负错误此异常:

  private void exportToExcel_Click(object sender,EventArgs e)
{
try
{
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook工作簿= app.Workbooks.Add(Type.Missing);
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
app.Visible = true;
工作表= workbook.Sheets [Sheet1];
工作表= workbook.ActiveSheet;
worksheet.Name =记录;

try
{
for(int i = 1; i< transcationTableDataGridView.Columns.Count + 1; i ++)
{
worksheet.Cells [1,i] = transcationTableDataGridView.Columns [i - 1] .HeaderText; (int i = 0; i< transcationTableDataGridView.Rows.Count - 1; i ++)
{
for(int j = 0; j< transcationTableDataGridView。 Columns.Count; j ++)
{
if(transcationTableDataGridView.Rows [i] .Cells [j] .Value!= null)
{
worksheet.Cells [i + 2 ,j + 1] = transcationTableDataGridView.Rows [i] .Cells [j] .Value.ToString();
}
else
{
工作表Cells [i + 2,j + 1] =;
}
}
}

//获取excel的位置和文件名,以保存用户。
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.Filter =Excel文件(* .xlsx)| * .xlsx |所有文件(*。*)| *。*;
saveDialog.FilterIndex = 2;

if(saveDialog.ShowDialog()== System.Windows.Forms.DialogResult.OK)
{
workbook.SaveAs(saveDialog.FileName);
MessageBox.Show(导出成功,成功,MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
catch(System.Exception ex)
{
MessageBox.Show(ex.Message);
}

finally
{
app.Quit();
workbook = null;
工作表= null;
}
}
catch(Exception ex){MessageBox.Show(ex.Message.ToString()); }
}

在某些计算机的导出过程中, strong>



我使用这段代码:

  Microsoft.Office.Interop.Excel; 

代码:

 使用System.Runtime.InteropServices; 

任何人都可以解释两者之间有什么区别?

解决方案

  private void exportToExcel_Click(object sender,EventArgs e)
{
try
{
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook工作簿= app.Workbooks.Add(Type.Missing);
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
app.Visible = true;
工作表= workbook.Sheets [Sheet1];
工作表= workbook.ActiveSheet;
worksheet.Name =记录;

try
{
for(int i = 0; i< transcationTableDataGridView.Columns.Count; i ++)
{
worksheet.Cells [1 ,i + 1] = transcationTableDataGridView.Columns [i] .HeaderText; (int i = 0; i< transcationTableDataGridView.Rows.Count; i ++)
{
for(int j = 0; j< transcationTableDataGridView.Columns) Count; j ++)
{
if(transcationTableDataGridView.Rows [i] .Cells [j] .Value!= null)
{
worksheet.Cells [i + 2,j + 1] = transcationTableDataGridView.Rows [i] .Cells [j] .Value.ToString();
}
else
{
工作表Cells [i + 2,j + 1] =;
}
}
}

//获取excel的位置和文件名,以保存用户。
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.Filter =Excel文件(* .xlsx)| * .xlsx |所有文件(*。*)| *。*;
saveDialog.FilterIndex = 2;

if(saveDialog.ShowDialog()== System.Windows.Forms.DialogResult.OK)
{
workbook.SaveAs(saveDialog.FileName);
MessageBox.Show(导出成功,成功,MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
catch(System.Exception ex)
{
MessageBox.Show(ex.Message);
}

finally
{
app.Quit();
workbook = null;
工作表= null;
}
}
catch(Exception ex){MessageBox.Show(ex.Message.ToString()); }
}


This code always skips last row after exporting excel, can you check what's wrong in the code?

I changed

transcationTableDataGridView.Rows.Count - 1

to

transcationTableDataGridView.Rows.Count + 1

it does export all rows to excel but throws index should be non-negative error this exception:

 private void exportToExcel_Click(object sender, EventArgs e)
        {
try
            {
                Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
                Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
                app.Visible = true;
                worksheet = workbook.Sheets["Sheet1"];
                worksheet = workbook.ActiveSheet;
                worksheet.Name = "Records";

                try
                {
                    for (int i = 1; i < transcationTableDataGridView.Columns.Count + 1; i++)
                    {
                        worksheet.Cells[1, i] = transcationTableDataGridView.Columns[i - 1].HeaderText;
                    }
                    for (int i = 0; i < transcationTableDataGridView.Rows.Count - 1; i++)
                    {
                        for (int j = 0; j < transcationTableDataGridView.Columns.Count; j++)
                        {
                            if (transcationTableDataGridView.Rows[i].Cells[j].Value != null)
                            {
                                worksheet.Cells[i + 2, j + 1] = transcationTableDataGridView.Rows[i].Cells[j].Value.ToString();
                            }
                            else
                            {
                                worksheet.Cells[i + 2, j + 1] = "";
                            }
                        }
                    }   

                    //Getting the location and file name of the excel to save from user. 
                    SaveFileDialog saveDialog = new SaveFileDialog();
                    saveDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*";
                    saveDialog.FilterIndex = 2;

                    if (saveDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        workbook.SaveAs(saveDialog.FileName);
                        MessageBox.Show("Export Successful", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

                finally
                {
                    app.Quit();
                    workbook = null;
                    worksheet = null;
                }    
            }
            catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }
        }

It also gives me error during export in some computers

I was using this code:

using Excel=Microsoft.Office.Interop.Excel;

to this code:

using System.Runtime.InteropServices;

can anyone explain what is the difference between two?

解决方案

    private void exportToExcel_Click(object sender, EventArgs e)
    {
        try
        {
            Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
            Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
            app.Visible = true;
            worksheet = workbook.Sheets["Sheet1"];
            worksheet = workbook.ActiveSheet;
            worksheet.Name = "Records";

            try
            {
                for (int i = 0; i < transcationTableDataGridView.Columns.Count; i++)
                {
                    worksheet.Cells[1, i + 1] = transcationTableDataGridView.Columns[i].HeaderText;
                }
                for (int i = 0; i < transcationTableDataGridView.Rows.Count; i++)
                {
                    for (int j = 0; j < transcationTableDataGridView.Columns.Count; j++)
                    {
                        if (transcationTableDataGridView.Rows[i].Cells[j].Value != null)
                        {
                            worksheet.Cells[i + 2, j + 1] = transcationTableDataGridView.Rows[i].Cells[j].Value.ToString();
                        }
                        else
                        {
                            worksheet.Cells[i + 2, j + 1] = "";
                        }
                    }
                }

                //Getting the location and file name of the excel to save from user. 
                SaveFileDialog saveDialog = new SaveFileDialog();
                saveDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*";
                saveDialog.FilterIndex = 2;

                if (saveDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    workbook.SaveAs(saveDialog.FileName);
                    MessageBox.Show("Export Successful", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            finally
            {
                app.Quit();
                workbook = null;
                worksheet = null;
            }
        }
        catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }
    }

这篇关于在C#中将Datagridview导出到Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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