PDA

View Full Version : سوال: مشکل با خروجی اکسل از دیتاگرید(ردیف اول در خروجی گرفته نمیشود)



moferferi
سه شنبه 25 مهر 1391, 21:08 عصر
با سلام
چند روز پیش دنبال گرفتن خروجی اکسل از دیتاگرید بودم که به سورس زیر در سایت دست پیدا کردم
// 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 < radGridView1.Columns.Count + 1; i++)
{

worksheet.Cells[1, i] = radGridView1.Columns[i - 1].HeaderText;

}

// storing Each row and column value to excel sheet

for (int i = 0; i < radGridView1.Rows.Count -1; i++)
{

for (int j = 0; j < radGridView1.Columns.Count; j++)
{

worksheet.Cells[i + 2, j + 1] = radGridView1.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();

ولی در گزارش ردیف اول گرید ویو را نادیده میگیره و اون را توی اکسل نمیاره.
به صورتی که اگر گرید فقط یک ردیف داشته باشه توی خروجی اکسل هیچ چیزی نیست.