0

我想将数据网格信息导出到 excel 文件。我的问题是我需要在要导出的每个窗口中重写相同的函数。是否可以将数据网格信息发送到主函数?

代码是:

private void button2_Click(object sender, EventArgs e)
    {
        string name = textBox1.Text;
        Excel.Application xlApp;
        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet;
        object misValue = System.Reflection.Missing.Value;

        xlApp = new Excel.Application();
        xlWorkBook = xlApp.Workbooks.Add(misValue);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
        int i = 0;
        int j = 0;

        for (i = 0; i <= dgvCostumers.RowCount - 1; i++)
        {
            for (j = 0; j <= dgvCostumers.ColumnCount - 1; j++)
            {
                DataGridViewCell cell = dgvCostumers[j, i];
                xlWorkSheet.Cells[i + 1, j + 1] = cell.Value;
            }
        }

        xlWorkBook.SaveAs(name + ".xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
        xlWorkBook.Close(true, misValue, misValue);
        xlApp.Quit();

        releaseObject(xlWorkSheet);
        releaseObject(xlWorkBook);
        releaseObject(xlApp);

        MessageBox.Show("Excel file created , you can find the file c:\\my documents\\" + name + ".xls");

        textBox1.Text = "";
        textBox1.Visible = false;
        button1.Visible = true;
        label2.Visible = false;
    }
}


private void releaseObject(object obj)
    {
        try
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
            obj = null;
        }
        catch (Exception ex)
        {
            obj = null;
            MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
        }
        finally
        {
            GC.Collect();
        }
    }
4

1 回答 1

0

像这样的东西怎么样

private void DataGridViewToExcel(string name, DataGridView dgv)
{
    Excel.Application xlApp; 
    Excel.Workbook xlWorkBook; 
    Excel.Worksheet xlWorkSheet; 
    object misValue = System.Reflection.Missing.Value; 

    xlApp = new Excel.Application(); 
    xlWorkBook = xlApp.Workbooks.Add(misValue); 
    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); 
    int i = 0; 
    int j = 0; 

    for (i = 0; i <= dgv.RowCount - 1; i++) 
    { 
        for (j = 0; j <= dgv.ColumnCount - 1; j++) 
        { 
            DataGridViewCell cell = dgv[j, i]; 
            xlWorkSheet.Cells[i + 1, j + 1] = cell.Value; 
        } 
    } 

    xlWorkBook.SaveAs(name + ".xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); 
    xlWorkBook.Close(true, misValue, misValue); 
    xlApp.Quit(); 

    releaseObject(xlWorkSheet); 
    releaseObject(xlWorkBook); 
    releaseObject(xlApp);  
}

然后您的按钮单击代码可以更改为类似

private void button2_Click(object sender, EventArgs e) 
{ 
        string name = textBox1.Text; 
        DataGridViewToExcel(name, dgvCostumers);

        MessageBox.Show("Excel file created , you can find the file c:\\my documents\\" + name + ".xls"); 

        textBox1.Text = ""; 
        textBox1.Visible = false; 
        button1.Visible = true; 
        label2.Visible = false; 
    } 
} 
于 2012-09-21T06:44:22.683 回答