PDA

Vollständige Version anzeigen : Daten nach Excel Vb.net


Serge
22.09.2003, 08:41
Hi.
Kann mir jemand dabei helfen die Daten aus einem Dataset nach Excel zu exportieren.Ich kriegs nit hin.

Nemesis
22.09.2003, 10:23
Hab das gleiche Problem nur unter VC++ .net! Also falls du es geschaft hast so poste mal bitte den Code dafür (falls jemand den für VC++ .net kennt dann bitte eben den)!

Serge
22.09.2003, 10:46
using System

using System.Configuration

using System.Data

using AcademicReport.DataAccessLayer //MS Data Access Application Blocks

using Excel = Microsoft.Office.Interop.Excel

using ExcelApplication = Microsoft.Office.Interop.Excel.Application

public class ExcelAutomation

{

// Frequenty-used variable for optional arguments.

private object xlsOpt = System.Reflection.Missing.Value

private string sXLSFileName

private Excel.Workbooks xlsBooks = null

private Excel._Workbook xlsBook = null

private Excel.Sheets xlsSheets = null

private Excel._Worksheet xlsSheet = null

private Excel.Range xlsRange = null

private Excel.Font xlsFont = null

private ExcelApplication excelApp

public ExcelApplication ExcelApp

{

get { return excelApp }

}


public ExcelAutomation

{

excelApp = new ExcelApplication

}


public void ExampleAutomation

{

excelApp.DisplayAlerts = false

excelApp.SheetsInNewWorkbook = 1


int err = 0

//Get some data - you need to change this

DataSet ds =
SqlHelper.ExecuteDatasetConfigurationSettings.AppSettings[Local.Connection
String],

usp_Report, sMSAlias, dteStart, dteEnd, err


if err != 0

{

throw new ExceptionStored Procedure
usp_tbl_CBOVALUES_SelectAllWFieldIDLogic meldete Fehler: + err

}

xlsBooks = Excel.WorkbooksexcelApp.Workbooks

xlsBook = Excel._WorkbookxlsBooks.AddxlsOpt


xlsSheets = Excel.SheetsxlsBook.Worksheets


//get first sheet

xlsSheet = Excel._WorksheetxlsSheets.get_Item1

//rename it

xlsSheet.Name = NewSheetName


InsertHeadersds.Tables[0], A1, true

InsertDatads.Tables[0], A, 2


sXLSFileName = @C:\test.xls

xlsBook.SaveAssXLSFileName, xlsOpt, xlsOpt,

xlsOpt, xlsOpt, xlsOpt, Excel.XlSaveAsAccessMode.xlNoChange,

xlsOpt, xlsOpt, xlsOpt, xlsOpt, xlsOpt

xlsBook.Closefalse, xlsOpt, xlsOpt

excelApp.DisplayAlerts = true

excelApp.Quit

}


private void InsertHeaders DataTable dtIn, string sStartCell, bool
bAutosize

{

//DataTable fields count

int nFields = dtIn.Columns.Count

//DataTable rows count

int nRows = dtIn.Rows.Count


//header array for the sheet header line

object[] objHeaders = new object[nFields]


forint i = 0 i nFields i++

{

//fill the array with field names

objHeaders[i] = dtIn.Columns[i].ColumnName

}


xlsRange = xlsSheet.get_RangesStartCell, xlsOpt

xlsRange = xlsRange.get_Resize1, nFields

xlsRange.set_ValuexlsOpt,objHeaders

xlsFont = xlsRange.Font

xlsFont.Bold=true


if bAutosize

xlsRange.Columns.AutoFit


}


private void InsertData DataTable dtIn, string sStartColumn, int
nStartRow

{

//DataTable fields count

int nFields = dtIn.Columns.Count

//DataTable rows count

int nRows = dtIn.Rows.Count


string sActiveRow = sStartColumn


for int n = 0 n nRows n++

{

sActiveRow = sStartColumn + n+nStartRow.ToString

xlsRange = xlsSheet.get_RangesActiveRow, xlsOpt

xlsRange = xlsRange.get_Resize1, nFields

xlsRange.set_ValuexlsOpt,dtIn.Rows[n].ItemArray

}

}


private void InsertData DataRow drIn, string sStartCell

{

//DataRow fields count

int nFields = drIn.Table.Columns.Count


xlsRange = xlsSheet.get_RangesStartCell, xlsOpt

xlsRange = xlsRange.get_Resize1, nFields

xlsRange.set_ValuexlsOpt,drIn.ItemArray

}

}

Serge
22.09.2003, 10:51
Der gepostete Code ist nicht auf meinem Mist gewachsen habe ich aber so aus dem Netz.
Also ohne Gewähr und ausserdem hab ich selber null Plan von VC++