PDA

View Full Version : سوال: خروجی اکسل از datagridview با اطلاعات فعلی در حال نمایش



reza4359
چهارشنبه 03 آذر 1395, 22: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);

}

mahdi.m.sh
جمعه 05 آذر 1395, 11:53 صبح
سلام


#region "Export Excel Code"

private void grid_ToExcel_Export(string FileName, DataGridView Data_GridView)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
try {
int intColumn = 0;
int intColumnValue = 0;
DataGridViewRow row = default(DataGridViewRow);
for (intColumn = 0; intColumn <= Data_GridView.Columns.Count - 2; intColumn++) {
sb.Append(Data_GridView.Columns(intColumn).HeaderT ext);
if (intColumnValue != Data_GridView.Columns.Count - 2) {
sb.Append(Constants.vbTab);
}
}
sb.Append(Constants.vbCrLf);
foreach ( row in Data_GridView.Rows) {
for (intColumnValue = 0; intColumnValue <= Data_GridView.Columns.Count - 2; intColumnValue++) {
sb.Append(Strings.StrConv((Information.IsDBNull(ro w.Cells(intColumnValue).Value) ? "" : row.Cells(intColumnValue).Value), VbStrConv.None));
if (intColumnValue != Data_GridView.Columns.Count - 2) {
sb.Append(Constants.vbTab);
}
}
sb.Append(Constants.vbCrLf);
}
SaveExcel(FileName, sb);
} catch (Exception ex) {
throw;
} finally {
Data_GridView = null;
sb = null;
}
}

private void SaveExcel(string fpath, System.Text.StringBuilder sb)
{
FileStream fsFile = new FileStream(fpath, FileMode.Create, FileAccess.Write);
StreamWriter strWriter = new StreamWriter(fsFile, System.Text.Encoding.Unicode);
try {
var _with1 = strWriter;
_with1.BaseStream.Seek(0, SeekOrigin.End);
_with1.WriteLine(sb);
_with1.Close();
} catch (Exception e) {
Interaction.MsgBox(e.ToString());
} finally {
sb = null;
strWriter = null;
fsFile = null;
}
}
public void grid_export_module_main(DataGridView dg)
{
SaveFileDialog oo = new SaveFileDialog();
try {
oo.Filter = "فایل اکسل|*.xls";
oo.Title = "ذخیره فایل اکسل";

if (oo.ShowDialog == System.Windows.Forms.DialogResult.OK) {
grid_ToExcel_Export(oo.FileName.Replace(".xls", "") + ".xls", dg);
MsgBoxStyle MsgFarsi = MsgBoxStyle.MsgBoxRight | MsgBoxStyle.Information;
PersianMsgbox.PatchMsgBox(new string[] { "تایید" });
if (Interaction.MsgBox("اطلاعات با موفقیت در اکسل ذخیره شد", MsgFarsi, "پیغام") == MsgBoxResult.Ok) {
}
}
} catch (Exception ex) {
Interaction.MsgBox(ex.Message);
}
}

#endregion




بعدش این کد رو داخل باتن مورد نظر می نویسی




grid_export_module_main(DataGridView1)