![]() |
|
|||||||
| Registrieren | Portal | Benutzerkontrollzentrum | Interessengemeinschaft | Mitglieder | Downloads | Alle Foren als gelesen markieren |
| Programmieren Hilfe rund um das Programmieren |
![]() |
|
|
Themen-Optionen | Ansicht |
|
|
#1 |
|
Registriert seit: Sep 2006
Ort: Sweet home, Chicago
Beiträge: 75
Abgegebene Danke: 0
Erhielt 0 Danke für 0 Beiträge
Downloads: 0
Uploads: 0
![]() |
Dieses tutorial ist immer noch in englisch, werde es aber bald auch uebersetzen. Falls ihr fragen habt, werden sie auch auf deutsch beantwortet ;-)
I have spent hours and hours of research to find a way to export the data from the gridview to an excel spreadsheet by using Visual Studio 2005, C# and Excel 2003. Most of the given examples are complicated and reference Visual Studio 2003. Microsoft's msdn library is way to complicated and, worst of all, their examples are hardcoded. *shakehead* This example served my purpose and I want to put it online, because I've seen many other desperate people trying to find a simple way to do this. I assume that you were able to connect to your database and retrieve the results. I created an ExcelClass where I instantiate an object Excel. When a user clicks on the export button on my main form, I create an object of ExcelClass. Here is my Simple Excel Class: Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Excel = Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
namespace Dims
{
public class ExcelClass
{
private DataTable myData;
private Excel.Application myExcel;
int rowIndex = 1;
int colIndex = 0;
public ExcelClass(DataTable d)
{
myData = d;
GoForIt();
}
public void GoForIt()
{
myExcel = new Microsoft.Office.Interop.Excel.Application();
myExcel.Visible = true;
myExcel.Application.Workbooks.Add(true);
foreach (DataColumn col in myData.Columns)
{
colIndex++;
myExcel.Cells[1, colIndex] = col.ColumnName;
}
foreach (DataRow row in myData.Rows)
{
rowIndex++;
colIndex = 0;
foreach (DataColumn col in myData.Columns)
{
colIndex++;
myExcel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
}
}
}
}
}
Code:
private void btnExport_Click(object sender, EventArgs e)
{
ExcelClass yesBaby = new ExcelClass(data);
}
__________________
[SIGPIC]http://www.clicksmilies.com/s0105/verkleidung/costumed-smiley-033.gif[/SIGPIC] |
|
|
|
![]() |
| Lesezeichen |
| Themen-Optionen | |
| Ansicht | |
|
|
Ähnliche Themen
|
||||
| Thema | Autor | Forum | Antworten | Letzter Beitrag |
| [Info] Loveparade 2007 in Essen | Bahamut | Offtopic | 14 | 26.08.2007 19:23 |