Adnan
asked on
How to export all columns from datagridview to excel.....?!
Hi
I want to export all my columns from datagridview to excel..., if i export all columns from a dataset it works fine, but i want to export columns from my gridview....?
how can i do that?
I want to export all my columns from datagridview to excel..., if i export all columns from a dataset it works fine, but i want to export columns from my gridview....?
how can i do that?
Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
excel.Application.Workbooks.Add(true);
//System.Data.DataTable table = dsReconItems1.Tables[0];
int ColumnIndex = 0;
foreach (DataGridViewColumn col in ReconManuallyGridView.Columns)
{
ColumnIndex++;
System.Data.DataTable table = (DataTable)col;
excel.Cells[1, ColumnIndex] = table;
}
excel.Visible = true;
Worksheet worksheet = (Worksheet)excel.ActiveSheet;
worksheet.Activate();
}
ASKER
hmm to complex to understand, i have tried but not sucecceded, any other sugestions?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Oki, i will check the link, but when i execute my code i get error message saying : Exception from HRESULT: 0x800A03EC.....?
private void ExcelTransactions()
{
Microsoft.Office.Interop.E xcel.Appli cationClas s excel = new Microsoft.Office.Interop.E xcel.Appli cationClas s();
excel.Application.Workbook s.Add(true );
//DsReconItems.MatchItemRo w currentAddMIRow = dsReconItems1.MatchItem.Fi ndByMatchI tem_ID(mat chItem_ID) ;
int ColumnIndex = 0;
//System.Data.DataTable myTable = new System.Data.DataTable();
//myTable = (System.Data.DataTable)dsR econItems1 .Tables[0] ;
//for (ColumnIndex = 0; ColumnIndex < ReconManuallyGridView.Colu mns.Count; ColumnIndex++)
//foreach (DataColumn col in myTable.Columns)
foreach (DataGridViewColumn col in ReconManuallyGridView.Colu mns)
{
ColumnIndex++;
//System.Data.DataTable table = (DataTable)col;
excel.Cells[1, ColumnIndex] = col;
}
int rowIndex = 0;
excel.Visible = true;
Worksheet worksheet = (Worksheet)excel.ActiveShe et;
worksheet.Activate();
}
private void ExcelTransactions()
{
Microsoft.Office.Interop.E
excel.Application.Workbook
//DsReconItems.MatchItemRo
int ColumnIndex = 0;
//System.Data.DataTable myTable = new System.Data.DataTable();
//myTable = (System.Data.DataTable)dsR
//for (ColumnIndex = 0; ColumnIndex < ReconManuallyGridView.Colu
//foreach (DataColumn col in myTable.Columns)
foreach (DataGridViewColumn col in ReconManuallyGridView.Colu
{
ColumnIndex++;
//System.Data.DataTable table = (DataTable)col;
excel.Cells[1, ColumnIndex] = col;
}
int rowIndex = 0;
excel.Visible = true;
Worksheet worksheet = (Worksheet)excel.ActiveShe
worksheet.Activate();
}
ASKER
Here is the correct answer and solution to what iam was asking you experts about, but gess what? i resolved it by my self... ;)
private void ExcelTransactions()
{
System.Data.DataTable table = dsReconItems1.Tables[0];
Microsoft.Office.Interop.E xcel.Appli cationClas s excel = new Microsoft.Office.Interop.E xcel.Appli cationClas s();
excel.Application.Workbook s.Add(true );
Worksheet worksheet = (Worksheet)excel.ActiveShe et;
int ColumnIndex = 0;
foreach (DataColumn col in table.Columns)
{
ColumnIndex++;
//excel.Cells[1, ColumnIndex] = col.ColumnName;
// col.ColumnName = "VDate";
worksheet.Cells[1, ColumnIndex = 1] = "VDate";
worksheet.Cells[1, ColumnIndex = 2] = "Ref";
worksheet.Cells[1, ColumnIndex = 3] = "Amount";
worksheet.Cells[1, ColumnIndex = 4] = "Text";
worksheet.Cells[1, ColumnIndex = 5] = "Comment";
worksheet.Cells[1, ColumnIndex = 6] = "AccountNo";
Microsoft.Office.Interop.E xcel.Range myCell1 = (Microsoft.Office.Interop. Excel.Rang e)workshee t.get_Rang e("A1","B1 ");
Microsoft.Office.Interop.E xcel.Range myCell2 = (Microsoft.Office.Interop. Excel.Rang e)workshee t.get_Rang e("C1", "D1");
Microsoft.Office.Interop.E xcel.Range myCell3 = (Microsoft.Office.Interop. Excel.Rang e)workshee t.get_Rang e("E1", "F1");
Range rg = worksheet.get_Range("A1", "F1");
rg.Select();
//bold
rg.Font.Bold = true;
// font face
rg.Font.Name = "Arial";
//font size
rg.Font.Size = 8;
// text wrap
rg.WrapText = true;
// cell allignment
rg.HorizontalAlignment = Microsoft.Office.Interop.E xcel.Const ants.xlCen ter;
// color index
rg.Interior.ColorIndex = 15;
// font weight
rg.Borders.Weight = 4;
// Border Line style
//rg.Borders.LineStyle = Microsoft.Office.Interop.E xcel.Const ants.
// row height
rg.Cells.RowHeight = 15;
myCell1.Font.Bold = true;
myCell2.Font.Bold = true;
myCell3.Font.Bold = true;
}
int rowIndex = 0;
foreach (Guid matchItem_ID in checkedMatchItems)
{
DsReconItems.MatchItemRow currentAddMIRow = dsReconItems1.MatchItem.Fi ndByMatchI tem_ID(mat chItem_ID) ;
if (currentAddMIRow != null)
{
rowIndex++;
ColumnIndex = 0;
worksheet.Cells[rowIndex + 1, ColumnIndex = 1] = currentAddMIRow.VDate;
worksheet.Cells[rowIndex + 1, ColumnIndex = 2] = currentAddMIRow.Ref;
worksheet.Cells[rowIndex + 1, ColumnIndex = 3] = currentAddMIRow.Amount;
//worksheet.Cells[rowIndex + 2, ColumnIndex = 4] = currentAddMIRow.AllText;
//worksheet.Cells[rowIndex + 2, ColumnIndex = 5] = currentAddMIRow.AllText;
worksheet.Cells[rowIndex + 1, ColumnIndex = 6] = currentAddMIRow.AccountNo;
//foreach (DataRow row in table.Rows)
//{
// rowIndex++;
// ColumnIndex = 0;
// worksheet.Cells[rowIndex + 1, ColumnIndex = 1] = currentAddMIRow.VDate;
// //foreach (DataColumn col in table.Columns)
// //{
// // ColumnIndex++;
// // worksheet.Cells[rowIndex + 1, ColumnIndex = 1] = currentAddMIRow.VDate;
// //}
//}
}
}
excel.Visible = true;
worksheet.Activate();
}
private void ExcelTransactions()
{
System.Data.DataTable table = dsReconItems1.Tables[0];
Microsoft.Office.Interop.E
excel.Application.Workbook
Worksheet worksheet = (Worksheet)excel.ActiveShe
int ColumnIndex = 0;
foreach (DataColumn col in table.Columns)
{
ColumnIndex++;
//excel.Cells[1, ColumnIndex] = col.ColumnName;
// col.ColumnName = "VDate";
worksheet.Cells[1, ColumnIndex = 1] = "VDate";
worksheet.Cells[1, ColumnIndex = 2] = "Ref";
worksheet.Cells[1, ColumnIndex = 3] = "Amount";
worksheet.Cells[1, ColumnIndex = 4] = "Text";
worksheet.Cells[1, ColumnIndex = 5] = "Comment";
worksheet.Cells[1, ColumnIndex = 6] = "AccountNo";
Microsoft.Office.Interop.E
Microsoft.Office.Interop.E
Microsoft.Office.Interop.E
Range rg = worksheet.get_Range("A1", "F1");
rg.Select();
//bold
rg.Font.Bold = true;
// font face
rg.Font.Name = "Arial";
//font size
rg.Font.Size = 8;
// text wrap
rg.WrapText = true;
// cell allignment
rg.HorizontalAlignment = Microsoft.Office.Interop.E
// color index
rg.Interior.ColorIndex = 15;
// font weight
rg.Borders.Weight = 4;
// Border Line style
//rg.Borders.LineStyle = Microsoft.Office.Interop.E
// row height
rg.Cells.RowHeight = 15;
myCell1.Font.Bold = true;
myCell2.Font.Bold = true;
myCell3.Font.Bold = true;
}
int rowIndex = 0;
foreach (Guid matchItem_ID in checkedMatchItems)
{
DsReconItems.MatchItemRow currentAddMIRow = dsReconItems1.MatchItem.Fi
if (currentAddMIRow != null)
{
rowIndex++;
ColumnIndex = 0;
worksheet.Cells[rowIndex + 1, ColumnIndex = 1] = currentAddMIRow.VDate;
worksheet.Cells[rowIndex + 1, ColumnIndex = 2] = currentAddMIRow.Ref;
worksheet.Cells[rowIndex + 1, ColumnIndex = 3] = currentAddMIRow.Amount;
//worksheet.Cells[rowIndex
//worksheet.Cells[rowIndex
worksheet.Cells[rowIndex + 1, ColumnIndex = 6] = currentAddMIRow.AccountNo;
//foreach (DataRow row in table.Rows)
//{
// rowIndex++;
// ColumnIndex = 0;
// worksheet.Cells[rowIndex + 1, ColumnIndex = 1] = currentAddMIRow.VDate;
// //foreach (DataColumn col in table.Columns)
// //{
// // ColumnIndex++;
// // worksheet.Cells[rowIndex + 1, ColumnIndex = 1] = currentAddMIRow.VDate;
// //}
//}
}
}
excel.Visible = true;
worksheet.Activate();
}
I think I did provided the links that should have resolved the original issue -
>>Hi
>>I want to export all my columns from datagridview to excel...,
I deserve some points for the links.
>>Hi
>>I want to export all my columns from datagridview to excel...,
I deserve some points for the links.
ASKER
thanks.. ;)
http://www.codeproject.com