Support Community

Forum Spielhalle Kalender Mitgliederkarte Alle Foren F.A.Q Kontakt Impressum
Zurück   support-community.net - Support rund um ihren Computer > Software > Programmieren
Registrieren Portal Benutzerkontrollzentrum Interessengemeinschaft Mitglieder Downloads Alle Foren als gelesen markieren

Programmieren Hilfe rund um das Programmieren

Antwort
 
Themen-Optionen Ansicht
Alt 18.10.2006, 05:43   #1
Supergirl
EhrenMod

 
Benutzerbild von Supergirl
 

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
Supergirl ist Aufstrebend
Smile DataTable Exportieren von C# 2005 nach Excel 2003 und 2007

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();
                }
            }
        }
    }
}
Here is where I create an object:

Code:
private void btnExport_Click(object sender, EventArgs e)
        {
            ExcelClass yesBaby = new ExcelClass(data);
        }
I hope it will be of use to someone.
__________________
[SIGPIC]http://www.clicksmilies.com/s0105/verkleidung/costumed-smiley-033.gif[/SIGPIC]
Supergirl ist offline   Mit Zitat antworten Nach Oben Nach unten
Anzeige in eigener Sache
Antwort

Lesezeichen

Themen-Optionen
Ansicht

Forumregeln
Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.

Gehe zu

Ähnliche Themen
Thema Autor Forum Antworten Letzter Beitrag
[Info] Loveparade 2007 in Essen Bahamut Offtopic 14 26.08.2007 19:23


Alle Zeitangaben in WEZ +1. Es ist jetzt 23:55 Uhr.


Powered by vBulletin® Version 3.8.6 (Deutsch)
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Template-Modifikationen durch TMS
eMule-Onlinehilfe.net + support-community.net
Anzeige in eigener Sache