PDA

View Full Version : مشکل در انتقال کلیه اطلاعات دیتاگرید ویو به اکسل



pure_love
دوشنبه 24 آذر 1399, 13:58 عصر
سلام بچه ها
من میخوام کلیه اطلاعات دیتاگرید ویو انتقال بدم به اکسل، از این کد استفاده کردم :


// creating Excel Application
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
// creating new WorkBook within Excel application
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
// creating new Excelsheet in workbook
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
// see the excel sheet behind the program
app.Visible = true;
// get the reference of first sheet. By default its name is Sheet1.
// store its reference to worksheet
worksheet = workbook.Sheets["Sheet1"];
worksheet = workbook.ActiveSheet;
// changing the name of active sheet
worksheet.Name = "Exported from gridview";
// storing header part in Excel
for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
}
// storing Each row and column value to excel sheet
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
}
// save the application
workbook.SaveAs("c:\\output.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode. xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
// Exit from the application
app.Quit();



ولی این پیغام میده :

152650

تذکر : ردیف اول هم درون اکسل نمایش میده ، ولی ردیف های دیگه نشون نمیده و این پیغام میده!

لطفا راهنمایی کنید

telegram
دوشنبه 24 آذر 1399, 15:00 عصر
سلام...
شما اول بیا یه تست انجام بده...
مقادیر رو به صورت ثابت پاس بده به اکسل ببین میاد یا نه...
ساید مشکل از خواندن اطلاعات از دیتاگریدتون باشه

pure_love
دوشنبه 24 آذر 1399, 15:37 عصر
سلام...
شما اول بیا یه تست انجام بده...
مقادیر رو به صورت ثابت پاس بده به اکسل ببین میاد یا نه...
ساید مشکل از خواندن اطلاعات از دیتاگریدتون باشه


یعنی چی ؟ میشه کدش بنویسید و کامل راهنمایی کنید

akrami123
دوشنبه 24 آذر 1399, 18:15 عصر
سلام بنظر بعلت null بودن سلول با خطا مواجه شده اید لذا با بررسی مقدار سلول با مشکلی مواجه نمی شوید



// storing Each row and column value to excel sheet
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
if (dataGridView1.Rows[i].Cells[j].Value != null)
worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
}

pure_love
سه شنبه 25 آذر 1399, 08:31 صبح
سلام بنظر بعلت null بودن سلول با خطا مواجه شده اید لذا با بررسی مقدار سلول با مشکلی مواجه نمی شوید



// storing Each row and column value to excel sheet
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
if (dataGridView1.Rows[i].Cells[j].Value != null)
worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
}




بازم همون پیغام میده،
اکسل باز میشه و ردیف اول دیتاگرید ویو هم داخلش میاد ، ولی ردیف های دیگه نمیاد ! و این پیغام میده که عکسش گذاشته بودم

pure_love
چهارشنبه 26 آذر 1399, 09:24 صبح
کسی نیست راهنمایی کنه ؟

pure_love
شنبه 29 آذر 1399, 11:00 صبح
بچه ها لطفا راهنمایی کنید خیلی نیاز دارم

pure_love
چهارشنبه 17 دی 1399, 07:39 صبح
بچه ها خیلی کارم گیره
لطفا یه نفر راهنمایی کنه

M.sheaikhzadeh
چهارشنبه 17 دی 1399, 09:02 صبح
از کد زیر استفاده کن

try
{
if (dataGridView1.RowCount > 0)
{
// creating Excel Application
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
// creating new WorkBook within Excel application
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
// creating new Excelsheet in workbook
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
// Preview the output file before saving
app.Visible = false; //true
// get the reference of first sheet. By default its name is Sheet1.
worksheet = workbook.Sheets["Sheet1"];
// store its reference to worksheet
worksheet = workbook.ActiveSheet;
// changing the name of active sheet
//worksheet.Name = "Exported from gridview";
// storing header part in Excel
for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
}
// storing Each row and column value to excel sheet
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
// Align each field using a loop
// worksheet.Cells[i + 2, j + 1].Style.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignRi ght;
}
}
//Text alignment within all fields
//worksheet.Cells.Style.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCe nter;
//Fold right and left fold Excel
//app.DefaultSheetDirection = (int)Excel.Constants.xlRTL; //or xlRTL
//worksheet.DisplayRightToLeft = true;
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Excel Documents (*.xlsx)|*.xlsx";
sfd.FileName = "Export.xlsx";
if (sfd.ShowDialog() == DialogResult.OK)
{
// save the application
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);
// Exit from the application
app.Quit();
MessageBox.Show("فایل اطلاعات ذخیره شد", "پیغام", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{

}
}
else
{
MessageBox.Show("عملیات انجام نشد", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception)
{
MessageBox.Show("عملیات انجام نشد", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error);
throw;
}

MasoudAdmin
شنبه 20 دی 1399, 07:31 صبح
سلام
از این کد هم می تونید کمک بگیرید:


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;
_WorkSheet = _WorkBook.Sheets["Sheet1"];
_WorkSheet = _WorkBook.ActiveSheet;
_WorkSheet.Name = "ErrorList";
_WorkSheet.DisplayRightToLeft = true;
_WorkSheet.Cells.Font.Name = "Tahoma";
_WorkSheet.Cells.NumberFormat = "@";
for (int i = 1; i < this.dgvErrorByAction.Columns.Count + 1; i++)
{
_WorkSheet.Cells[1, i] = dgvErrorByAction.Columns[i - 1].HeaderText;
}
for (int i = 0; i < this.dgvErrorByAction.Rows.Count; i++)
{
for (int j = 0; j < this.dgvErrorByAction.Columns.Count; j++)
{
_WorkSheet.Cells[i + 2, j + 1] = this.dgvErrorByAction.Rows[i].Cells[j].Value.ToString();
}
}
var _SaveFileDialog = new SaveFileDialog();
_SaveFileDialog.FileName = "ErrorList_" + Today + "_" + DateTime.Now.Hour.ToString("00") + "_" + DateTime.Now.Minute.ToString("00");
_SaveFileDialog.DefaultExt = ".xls";
if (_SaveFileDialog.ShowDialog() == DialogResult.OK)
{
_WorkBook.SaveAs(_SaveFileDialog.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);
}
_App.Quit();

M.sheaikhzadeh
شنبه 20 دی 1399, 07:49 صبح
سلام
از این کد هم می تونید کمک بگیرید:


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;
_WorkSheet = _WorkBook.Sheets["Sheet1"];
_WorkSheet = _WorkBook.ActiveSheet;
_WorkSheet.Name = "ErrorList";
_WorkSheet.DisplayRightToLeft = true;
_WorkSheet.Cells.Font.Name = "Tahoma";
_WorkSheet.Cells.NumberFormat = "@";
for (int i = 1; i < this.dgvErrorByAction.Columns.Count + 1; i++)
{
_WorkSheet.Cells[1, i] = dgvErrorByAction.Columns[i - 1].HeaderText;
}
for (int i = 0; i < this.dgvErrorByAction.Rows.Count; i++)
{
for (int j = 0; j < this.dgvErrorByAction.Columns.Count; j++)
{
_WorkSheet.Cells[i + 2, j + 1] = this.dgvErrorByAction.Rows[i].Cells[j].Value.ToString();
}
}
var _SaveFileDialog = new SaveFileDialog();
_SaveFileDialog.FileName = "ErrorList_" + Today + "_" + DateTime.Now.Hour.ToString("00") + "_" + DateTime.Now.Minute.ToString("00");
_SaveFileDialog.DefaultExt = ".xls";
if (_SaveFileDialog.ShowDialog() == DialogResult.OK)
{
_WorkBook.SaveAs(_SaveFileDialog.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);
}
_App.Quit();


کدتون دو تا مشکل داره
اول تو خط 22 عبارت Today مشخص نیست متغیره یا رشته!
دوم فرمت فایل خروجی تو خط 23 هم باید xlsx تعریف بشه!

MasoudAdmin
یک شنبه 21 دی 1399, 07:39 صبح
ToDay متغیر هست. شما میتونید استفاده نکنید.
xls هم به xlsx تغییر بدید.
این کدی بوده که من تو یه برنامه ای که نیاز داشتم جواب گرفتم.