lunes, febrero 16, 2009

DataSet a Excel

Codigo simple para pasar de un DataSet a un archivo excel.

using Office = Microsoft.Office.Core;
using Excel;

[...]

private static void DataSetToExcel(DataSet ds, Boolean generateIdentity)
{
Excel.Application xlApp = new Excel.Application();
xlApp.Visible = false;
Workbook wb = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
for (int k = 0; k <>
{
System.Data.DataTable dt = ds.Tables[k];
Worksheet ws = (Worksheet) wb.Worksheets.Add(Missing.Value,
Missing.Value, Missing.Value, Missing.Value);
ws.Name = dt.TableName;

for (int i = 0; i <>
{
for (int j = 0; j <>
{
if (i == 0)
ws.Cells[1, j + 1] = dt.Columns[j].ColumnName;

ws.Cells[i + 2, j + 1] = (j == 0 && generateIdentity) ?
(i + 1).ToString() : dt.Rows[i][j].ToString();
}
}
}
xlApp.Visible = true;

Se necesita una referencia a Microsoft Excel 11.0 Object Library
}

Etiquetas:

martes, febrero 10, 2009

Publicar un COM en la GAC

Para hacer visible un COM en la GAC es necesario primero crear un wapper signado para el COM en .NET
Para ello hay que crear la key:

c:\>sn -k thekey.snk

Y luego importar los tipos del COM dentro del wrapper:

c:\>tlbimp COM.dll /out:myAssembly.dll /keyfile:thekey.snk

Una vez generado el wrapper, solo hay que instalarlo en la GAC por gacutil o un MSI

Etiquetas: ,