如何将数据表的每一列复制并粘贴到ASP.NET中的Excel

问题描述:



我使用下面的代码一次将数据集的值复制到预定义的Excel工作表一个单元格.这很耗时.
有什么方法可以从数据集中复制列并将其分配给excel列范围吗?

< 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 ++)
{

字符串行= 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 =行;
}


}</pre>

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>

导出比复制粘贴要好-请参见 ^ ].
Its better to export than to copy paste - see Code Project - Frequently Asked Questions Series 1: The ASP.Net GridView[^].


查看此链接可能对您有所帮助

http://www.excel-spreadsheet -authors.com/how-to-convert-a-data-table-to-a-range.html/ [
check out this link it may help u

http://www.excel-spreadsheet-authors.com/how-to-convert-a-data-table-to-a-range.html/[^]


参考和名称空间:
使用Microsoft.Office.Interop.Excel;
使用Microsoft.Office.Interop.InfoPath.Xml;
使用System.Xml.Xsl;
使用System.Xml;



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

//为FileSteam创建XmlTextWriter
//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 =新的System.Xml.XmlTextWriter(fs,System.Text.Encoding.Unicode);

//将处理指令添加到XML文件的开头,X
//其中之一表示样式表.
xtw.WriteProcessingInstruction("xml","version =" 1.0");
字符串strXSLFilename;
strXSLFilename ="test.xsl";
xtw.WriteProcessingInstruction("xml-stylesheet","type =" text/xls"href =""+ strXSLFilename +"'');

//将XML从数据集中写入文件中
CSVDtls.WriteXml(xtw);
xtw.Close(); </pre>
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>