0

我有一个 Windows c# 应用程序,它有一个显示访问数据库的 dataGridview。当我单击“报告”按钮时,我希望显示一个小的弹出表单,用于检查管理员密码,如果经过身份验证,则应将 gridview 导出到 Excel 表。然而,这并没有发生,因为我猜 gridview 和 login 是两种不同的形式。

 private void button1_Click(object sender, EventArgs e)
    {

        try
        {

            OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\dell\Documents\Database5.accdb;Jet OLEDB:Database Password=sudeep;");
            con.Open();
            OleDbCommand cmd = new OleDbCommand("select * from Login where id='" + textBox1.Text + "' and pass='" + textBox2.Text + "'", con);
            OleDbDataReader dr = cmd.ExecuteReader();
            if (dr.Read() == true)
            {
                //MessageBox.Show("Login Successful");
                this.Visible = false;
                //Form4 frm = new Form4();
                //frm.Show();
                //Form1 fr = new Form1();
                //fr.Dispose(true);
                Form2 frm2 = new Form2();
                frm2.Show();

                this.Close();
                System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadProc1));
                t.Start();



                Microsoft.Office.Interop.Excel.Application ExcelApp =
         new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel._Workbook ExcelBook;
                Microsoft.Office.Interop.Excel._Worksheet ExcelSheet;

                int i = 0;
                int j = 0;

                //create object of excel
                ExcelBook = (Microsoft.Office.Interop.Excel._Workbook)ExcelApp.Workbooks.Add(1);
                ExcelSheet = (Microsoft.Office.Interop.Excel._Worksheet)ExcelBook.ActiveSheet;
                //export header
                for (i = 1; i <= frm2.dgv.Columns.Count; i++)
                {
                    ExcelSheet.Cells[1, i] = frm2.dgv.Columns[i - 1].HeaderText;
                }

                //export data
                for (i = 1; i <= frm2.dgv.RowCount; i++)
                {
                    for (j = 1; j <= frm2.dgv.ColumnCount; j++)
                    {
                        ExcelSheet.Cells[i + 1, j] = frm2.dgv.Rows[i - 1].Cells[j - 1].Value;
                    }
                }

                ExcelApp.Visible = true;

                //set font Khmer OS System to data range
                Microsoft.Office.Interop.Excel.Range myRange = ExcelSheet.get_Range(
                                          ExcelSheet.Cells[1, 1],
                                          ExcelSheet.Cells[frm2.dgv.RowCount + 1,
                                          frm2.dgv.ColumnCount + 1]);
                Microsoft.Office.Interop.Excel.Font x = myRange.Font;
                x.Name = "Arial";
                x.Size = 10;

                //set bold font to column header
                myRange = ExcelSheet.get_Range(ExcelSheet.Cells[1, 1],
                                         ExcelSheet.Cells[1,frm2.dgv.Columns.Count]);
                x = myRange.Font;
                x.Bold = true;
                //autofit all columns
                myRange.EntireColumn.AutoFit();

                //
                ExcelSheet = null;
                ExcelBook = null;
                ExcelApp = null;


           }
            else
            {
                MessageBox.Show("Login as adminstrator to generate report");
            }
        }

我的 datagridview 在表格 2 上,我试图从表格 3 访问它,即登录表格我使用了公共 datagridview dgv {get;set} 命令,但应用程序仍然没有打开。请帮忙

4

2 回答 2

0

试试这个,而不是使用互操作,你可以这样做来生成可以被 excel 轻松读取的 CSV:

在 datagridview 所在的 form2 中执行以下操作:

public DataGridView DgReport = new DataGridView();

private void buttonReport_Click()
{
    //assuming your dataGridview is already populated
    DgReport = yourDataGridview;

    Form3 form3 = new Form3(this);
    form3.ShowDialog();
    form3.Dispose();
}

在表格 3 中:

private Form2 _form2 {get;set;}

public Form3(Form2 form2)
{
    _form2 = form2;
}


private buttonGenerateReport_Click()
{
    int columnsCount;

    var writer = new StreamWriter("test.csv");

    //get number of columns
    columnsCount = _form2.DgReport.Columns.Count;
    for (int i = 0; i < columnsCount - 1; i++)
    { 
        writer.Write(_form2.DgReport.Columns[i].Name.ToString().ToUpper() + ",");
    } 
    writer.WriteLine();

    //write on excel
    for (int i = 0; i < (_form2.DgReport.Rows.Count - 1); i++)
    { 
        for (int j = 0; j < columnsCount; j++)
        { 
            if (_form2.DgReport.Rows[i].Cells[j].Value != null)
            {
                writer.Write(_form2.DgReport.Rows[i].Cells[j].Value + ",");
            }
            else 
            {
                writer.Write(",");
            }
        }

        writer.WriteLine();
    }

    //close file
    writer.Close();
}
于 2013-09-28T06:16:24.727 回答
0

您可以在要使用 datagridview 的类中添加以下行:

System.Windows.Forms.Form f = System.Windows.Forms.Application.OpenForms["FormName"]; // form name where you have dgv

并通过以下方式调用 dgv:

((FormName)f).dgv.Rows.Count

在您想要获取 dgv 详细信息的位置更改此设置。试一次

于 2013-09-28T06:41:40.570 回答