reza4359
چهارشنبه 03 آذر 1395, 23:29 عصر
با سلام
دوستان من میخوام از dgv خروجی اکسل بگیرم
با همان حالت فعلی و اطلاعات نمایش داده شده
من از این کد استفاده میکنم ولی در اینجا باید اول دیتاسورس گرید ویو را با یک select پر کنی که خروجی بگیره وگرنه بهم خروجی نمیده
حالا باید چکار کرد؟
SqlConnection con230 = new SqlConnection();
con230.ConnectionString = strCon;
strSQL24 = "select * from tbl_ic ";
dataAdapter = new SqlDataAdapter(strSQL24, con230);
commandBuilder = new SqlCommandBuilder(dataAdapter);
table = new System.Data.DataTable();
dataAdapter.Fill(table);
dbBindSource1 = new BindingSource();
dbBindSource1.DataSource = table;
dataGridViewX5.DataSource = dbBindSource1;
object mis = System.Reflection.Missing.Value;
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 = false;
worksheet = (Worksheet)workbook.Sheets["Sheet1"];
worksheet = (Worksheet)workbook.ActiveSheet;
worksheet.Name = "Export";
for (int i = 1; i < dataGridViewX5.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = dataGridViewX5.Columns[i - 1].HeaderText;
}
for (int i = 0; i < dataGridViewX5.Rows.Count; i++)
{
for (int j = 0; j < dataGridViewX5.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = dataGridViewX5.Rows[i].Cells[j].Value.ToString();
}
}
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Excel Document(*.xlsx)|*.xlsx";
sfd.FileName = "Export";
if (sfd.ShowDialog() == DialogResult.OK)
{
workbook.SaveAs(sfd.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode. xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Msg.Eshow(" فایل اکسل با موفقیت ایجاد شد " + "", "پیغام سیستم", MsgIcon.Add, MsgButton.Ok, MsgDefualtButton.Button1, true, true, 30, Color.AliceBlue, Color.Green, .90, MsgBoarderStayle.ToolWindow);
}
دوستان من میخوام از dgv خروجی اکسل بگیرم
با همان حالت فعلی و اطلاعات نمایش داده شده
من از این کد استفاده میکنم ولی در اینجا باید اول دیتاسورس گرید ویو را با یک select پر کنی که خروجی بگیره وگرنه بهم خروجی نمیده
حالا باید چکار کرد؟
SqlConnection con230 = new SqlConnection();
con230.ConnectionString = strCon;
strSQL24 = "select * from tbl_ic ";
dataAdapter = new SqlDataAdapter(strSQL24, con230);
commandBuilder = new SqlCommandBuilder(dataAdapter);
table = new System.Data.DataTable();
dataAdapter.Fill(table);
dbBindSource1 = new BindingSource();
dbBindSource1.DataSource = table;
dataGridViewX5.DataSource = dbBindSource1;
object mis = System.Reflection.Missing.Value;
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 = false;
worksheet = (Worksheet)workbook.Sheets["Sheet1"];
worksheet = (Worksheet)workbook.ActiveSheet;
worksheet.Name = "Export";
for (int i = 1; i < dataGridViewX5.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = dataGridViewX5.Columns[i - 1].HeaderText;
}
for (int i = 0; i < dataGridViewX5.Rows.Count; i++)
{
for (int j = 0; j < dataGridViewX5.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = dataGridViewX5.Rows[i].Cells[j].Value.ToString();
}
}
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Excel Document(*.xlsx)|*.xlsx";
sfd.FileName = "Export";
if (sfd.ShowDialog() == DialogResult.OK)
{
workbook.SaveAs(sfd.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode. xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Msg.Eshow(" فایل اکسل با موفقیت ایجاد شد " + "", "پیغام سیستم", MsgIcon.Add, MsgButton.Ok, MsgDefualtButton.Button1, true, true, 30, Color.AliceBlue, Color.Green, .90, MsgBoarderStayle.ToolWindow);
}