The Engine Room
The engine room is a place for people to contribute ideas on the development of Sharepoint, Visual Studio and information technology.

How to bulk insert data into an excel worksheet using c# and Office 12

Going through each cell and updating its data is a takes time.

Excel allows data in an object[,] array to be inserted directly into a range object.

The following C# function inserts data from a dataset quickly.

 

 /// <summary>

/// Add data to the sheets, with the columns names at the top

/// </summary>

/// <param name="dataTable">The data to be added to the sheet</param>

/// <param name="sheetToAddTo">The worksheet to add the data to</param>

/// <returns>The range of the data that has been added</returns>

public static Range AddData(

System.Data.DataTable dataTable, Worksheet sheetToAddTo)

{

 //create the object to store the column names

 object[,] columnNames;

 columnNames = new object[1, dataTable.Columns.Count];

 //add the columns names from the datatable

 for (int i = 0; i < dataTable.Columns.Count; i++)

 {

columnNames[0, i] = dataTable.Columns[i].ColumnName;

 }

 //get a range object that the columns will be added to

 Range columnsNamesRange = sheetToAddTo.get_Range(sheetToAddTo.Cells[1, 1]

, sheetToAddTo.Cells[1, dataTable.Columns.Count]);

 //a simple assignement allows the data to be transferred quickly

 columnsNamesRange.Value2 = columnNames;

//release the columsn range object now it is finished with

System.Runtime.InteropServices.Marshal.ReleaseComObject(columnsNamesRange);

 columnsNamesRange = null;

 

 //create the object to store the dataTable data

 object[,] rowData;

 rowData = new object[dataTable.Rows.Count, dataTable.Columns.Count];

 //insert the data into the object[,]

 for (int iRow = 0; iRow < dataTable.Rows.Count; iRow++)

 {

for (int iCol = 0; iCol < dataTable.Columns.Count; iCol++)

{

rowData[iRow, iCol] = dataTable.Rows[iRow][iCol];

}

 }

 

 //get a range to add the table data into

 //it is one row down to avoid the previously added columns

Range dataCells = sheetToAddTo.get_Range(sheetToAddTo.Cells[2, 1],

sheetToAddTo.Cells[dataTable.Rows.Count + 1, dataTable.Columns.Count]);

 //assign data to worksheet

 dataCells.Value2 = rowData;

 

 //release range

 System.Runtime.InteropServices.Marshal.ReleaseComObject(dataCells);

 dataCells = null;

 

 //return the range to the new data

 return sheetToAddTo.get_Range(sheetToAddTo.Cells[1, 1],

sheetToAddTo.Cells[dataTable.Rows.Count + 1, dataTable.Columns.Count]);

}

 

Once you have the data in the table, call the following procedure to make a nice table.

/// <summary>

/// This function adds a new Excel table

/// (with nice formatting and filtering etc)

/// to the tableRange passed in. It names the range tableName.

/// This will error if tableName is already used.

/// This function uses XlYesNoGuess.xlYes to

/// automatically make the first row the headings.

/// </summary>

/// <param name="tableRange">Range to convert to a table</param>

/// <param name="tableName">Name of new table</param>

public static void Addtable(Range tableRange, string tableName)

{

Worksheet activeSheet =

(Worksheet)Globals.ThisAddIn.Application.ActiveSheet;

 ListObject newList = tableRange.Worksheet.ListObjects.Add(

XlListObjectSourceType.xlSrcRange, tableRange,

null, XlYesNoGuess.xlYes, tableRange);

 newList.Name = tableName;

}

Adding an embedded XSLT to your WebPart

In order to ensure an appropriate separation of your view, from your controller (Following the MVC design pattern), it is often desirable to keep your XSLT files separate from your dll’s.  Nevertheless, in some circumstances it is useful to encapsulate your XSLT files within your program dll; for instance, in situations where your program is performing one simple operation, and it is necessary to cleanly package this as a single redeployable executable.

When creating a Sharepoint WebPart, it is possible to either include a reference to an existing XSLT, or to package this XSLT as an embedded file.  By packaging your xslt’s together into the executable dll, we may simplify the deployment process.

To include an XSLT as an embedded resource: 

First, add the file to your project.  Second, use the solution explorer to navigate to your newly added XSLT file, right click, and select properties.  In the properties panel, click the “Build Action” drop-down box, and select “Embedded Resource”.

Your xslt file will now be included as an embedded resource file, in your program dll.

To reference this file from your code:

First, add the reflection namespace to your class.

             Using System.Reflection;

Second, create a reference to the currently executing assembly, and get the xslt resource by passing in the full name of the resource – this is of the format: [Assembly name].[Default nameapace].[resource name]

XslTransform xslTransform = new XslTransform();

Assembly currentAssembly = Assembly.GetExecutingAssembly();

// The following line assumes that both your assembly name, and namespace are set to

“SharepointWebPart”

XmlTextReader transformStream = new XmlTextReader

(currentAssembly.GetManifestResourceStream

(“SharepointWebPart.

SharepointWebPart.resourceFileName.xslt”));

xslTransform.Load(transformStream);

 

At this point, you have added an XSLT as an embedded resource, and utilised it in your code.  You may now compile your WebPart into a single deployable executable.