Exemplo n.º 1
0
        private void btnPrint_Click(object sender, EventArgs e)
        {
            frmSelectedColumn frm = new frmSelectedColumn(dgvEnquiry.Columns);

            if (frm.ShowDialog() == DialogResult.OK)
            {
                List <string> scs = frm.SelectedColumnNames;

                if (dgvEnquiry.Rows.Count == 0)
                {
                    return;
                }
                try
                {
                    //建立Excel对象
                    Excel.Application excel = new Excel.Application();
                    excel.Application.Workbooks.Add(true);
                    excel.Visible = true;
                    Excel.Worksheet sheet = (Excel.Worksheet)excel.Application.Worksheets[1];

                    sheet.Name = (dgvEnquiry.DataSource is DataTable) ?
                                 (dgvEnquiry.DataSource as DataTable).TableName : "表名";

                    //生成字段名称
                    for (int i = 0; i < scs.Count; i++)
                    {
                        excel.Cells[1, i + 1] = frm.SelectedColumnTexts[i];//dgvEnquiry.Columns[i].HeaderText;
                    }
                    //填充数据
                    for (int i = 0; i < dgvEnquiry.RowCount; i++)
                    {
                        for (int j = 0; j < scs.Count; j++)
                        {
                            if (dgvEnquiry.Rows[i].Cells[scs[j]].ValueType == typeof(string))
                            {
                                excel.Cells[i + 2, j + 1] = "'" + dgvEnquiry.Rows[i].Cells[scs[j]].Value.ToString();
                            }
                            else
                            {
                                excel.Cells[i + 2, j + 1] = dgvEnquiry.Rows[i].Cells[scs[j]].Value.ToString();
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }
Exemplo n.º 2
0
        private void btnPrint_Click(object sender, EventArgs e)
        {
            frmSelectedColumn frm = new frmSelectedColumn(dgvEnquiry.Columns);
            if (frm.ShowDialog() == DialogResult.OK)
            {
                List<string> scs = frm.SelectedColumnNames;

                if (dgvEnquiry.Rows.Count == 0)
                    return;
                try
                {
                    //建立Excel对象
                    Excel.Application excel = new Excel.Application();
                    excel.Application.Workbooks.Add(true);
                    excel.Visible = true;
                    Excel.Worksheet sheet = (Excel.Worksheet)excel.Application.Worksheets[1];

                    sheet.Name = (dgvEnquiry.DataSource is DataTable) ?
                        (dgvEnquiry.DataSource as DataTable).TableName : "表名";

                    //生成字段名称
                    for (int i = 0; i < scs.Count; i++)
                    {
                        excel.Cells[1, i + 1] = frm.SelectedColumnTexts[i];//dgvEnquiry.Columns[i].HeaderText;
                    }
                    //填充数据
                    for (int i = 0; i < dgvEnquiry.RowCount; i++)
                    {
                        for (int j = 0; j < scs.Count; j++)
                        {
                            if (dgvEnquiry.Rows[i].Cells[scs[j]].ValueType == typeof(string))
                            {
                                excel.Cells[i + 2, j + 1] = "'" + dgvEnquiry.Rows[i].Cells[scs[j]].Value.ToString();
                            }
                            else
                            {
                                excel.Cells[i + 2, j + 1] = dgvEnquiry.Rows[i].Cells[scs[j]].Value.ToString();
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }