Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I use the below code for copying the value of dataset to predefined excel sheet one cell at a time. This is time consuming.
Is there any possible way to copy a column from dataset and assign it to excel column range?

<pre lang="c#">for (int i = 1; i < ds.Tables[0].Columns.Count - 1; i++)
{
for (int j = 1; j < ds.Tables[0].Rows.Count - 1; j++)
{

string row = ds.Tables[0].Rows[j][i - 1].ToString();
Microsoft.Office.Interop.Excel.Range excelCell = (Microsoft.Office.Interop.Excel.Range)excelWorksheet.Cells[j + 9, k];
excelCell.Value2 = row;
}


}</pre>
Posted
Updated 2-Oct-11 23:29pm
v3

Its better to export than to copy paste - see Code Project - Frequently Asked Questions Series 1: The ASP.Net GridView[^].
 
Share this answer
 
v2
 
Share this answer
 
References and namespaces:
using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.InfoPath.Xml;
using System.Xml.Xsl;
using System.Xml;



<pre lang="c#">XmlDataDocument xmlDoc = new System.Xml.XmlDataDocument(CSVDtls);
XslCompiledTransform xslTran = new System.Xml.Xsl.XslCompiledTransform();

//Create XmlTextWriter for the FileSteam
//FileStream fs = new System.IO.FileStream("test.xls",FileMode.OpenOrCreate);
FileStream fs = new System.IO.FileStream(@"C:\EXPORT_Files\test.xls", FileMode.OpenOrCreate);

XmlTextWriter xtw = new System.Xml.XmlTextWriter(fs, System.Text.Encoding.Unicode);

//Add processing instructions to the beginning of the XML file,X
// one of which indicates a style sheet.
xtw.WriteProcessingInstruction("xml", "version='1.0'");
string strXSLFilename;
strXSLFilename = "test.xsl";
xtw.WriteProcessingInstruction("xml-stylesheet", "type='text/xls' href='" + strXSLFilename + "'");

//Write the XML from the dataset to the file
CSVDtls.WriteXml(xtw);
xtw.Close(); </pre>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900