DataSet怎样导出Excel文件? 有沒有winform那個DataGrid的例子啊,你的是WebForm的 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://www.yesky.com/20020701/1618411.shtmlhttp://www.cs-aspx.net //给你一个DataTable的你参考public bool TableToExcel(DataTable dt, string fileName, bool showTitle) { bool boolResult = false; System.IO.FileStream fsobj = null; System.IO.StreamWriter _sw = null; try { fsobj = new FileStream(fileName,System.IO.FileMode.Create,FileAccess.ReadWrite);//生成一个文件流 _sw = new StreamWriter(fsobj,System.Text.UnicodeEncoding.Unicode); //生成一个写入器 //写列标题 if(showTitle) { for(int i=0;i<dt.Columns.Count;i++) { _sw.Write("'"+dt.Columns[i].ColumnName+"\t"); } _sw.Write("\r"); } //写数据 for(int i=0;i<dt.Rows.Count;i++) { for(int j=0;j<dt.Columns.Count;j++) { _sw.Write(dt.Rows[i][j].ToString().Trim()+((char)2).ToString()+"\t"); } _sw.Write("\r"); } _sw.Close(); fsobj.Close(); boolResult = true; } catch(Exception er) { string a = er.Message; if(_sw!=null) { _sw.Close(); } if(fsobj!=null) { fsobj.Close(); } boolResult = false; } return boolResult; } using System.IO;string strSql="select * from piwsorg ";System.Data.DataTable tb;tb=(new clsSql()).gettblist(strSql);grd.DataSource=tb;grd.DataBind();int iRow;String filenew=Page.MapPath("ImportData/bbb.xls");FileStream f = new FileStream(filenew, FileMode.CreateNew, FileAccess.ReadWrite);StreamWriter fw = new StreamWriter(f, System.Text.Encoding.GetEncoding("GB2312"));String OutputString=""; int i;int[] arrCol=new int[] {0,1,3,5};foreach (int iCol in arrCol){OutputString = OutputString +"\t"+ tb.Columns[iCol].Caption;}OutputString = OutputString.Trim();fw.WriteLine(OutputString.Trim());fw.WriteLine("");for(iRow=0;iRow<=tb.Rows.Count-1;iRow++){ OutputString = ""; foreach (int iCol in arrCol) { OutputString = OutputString + "\t" + tb.Rows[iRow][iCol].ToString(); } fw.WriteLine(OutputString.Trim());}fw.Close();f.Close(); 把DataSet里的数据先写到文本文件里,再从文本里导出到Excel中 如果像你们这样搞个文本文件的话,我不如索性导出xml好了 http://www.xbee.org/pub/Temp/csharp/DataSetToExcel.txt c# 正则表达式求教 c# 帮忙给解释下问号的意思作用 框架中打开网页 WinForms VS Web 还是关于应用程序间通信的问题 c# win form 文本文件中的數據新增到數據庫...求解.............. 请教如何选择文件夹 怎么样实现像Office那样有格式地粘贴。 我要知道各位怎样写程序 遇到了一个怪问题,程序中有两个窗体,想在F1里运行F2,但怎么也实例不到 Form f2 = new (找不到F2)怎么回事啊? (十万火急)怎么把DataSet中的数据导入Excel???
http://www.cs-aspx.net
public bool TableToExcel(DataTable dt, string fileName, bool showTitle)
{
bool boolResult = false;
System.IO.FileStream fsobj = null;
System.IO.StreamWriter _sw = null;
try
{
fsobj = new FileStream(fileName,System.IO.FileMode.Create,FileAccess.ReadWrite);//生成一个文件流
_sw = new StreamWriter(fsobj,System.Text.UnicodeEncoding.Unicode); //生成一个写入器
//写列标题
if(showTitle)
{
for(int i=0;i<dt.Columns.Count;i++)
{
_sw.Write("'"+dt.Columns[i].ColumnName+"\t");
}
_sw.Write("\r");
}
//写数据
for(int i=0;i<dt.Rows.Count;i++)
{
for(int j=0;j<dt.Columns.Count;j++)
{
_sw.Write(dt.Rows[i][j].ToString().Trim()+((char)2).ToString()+"\t");
}
_sw.Write("\r");
} _sw.Close();
fsobj.Close();
boolResult = true;
}
catch(Exception er)
{
string a = er.Message;
if(_sw!=null)
{
_sw.Close();
}
if(fsobj!=null)
{
fsobj.Close();
}
boolResult = false;
} return boolResult;
}
string strSql="select * from piwsorg ";
System.Data.DataTable tb;
tb=(new clsSql()).gettblist(strSql);
grd.DataSource=tb;
grd.DataBind();
int iRow;
String filenew=Page.MapPath("ImportData/bbb.xls");
FileStream f = new FileStream(filenew, FileMode.CreateNew, FileAccess.ReadWrite);
StreamWriter fw = new StreamWriter(f, System.Text.Encoding.GetEncoding("GB2312"));
String OutputString="";
int i;
int[] arrCol=new int[] {0,1,3,5};
foreach (int iCol in arrCol)
{
OutputString = OutputString +"\t"+ tb.Columns[iCol].Caption;
}
OutputString = OutputString.Trim();
fw.WriteLine(OutputString.Trim());
fw.WriteLine("");
for(iRow=0;iRow<=tb.Rows.Count-1;iRow++)
{
OutputString = "";
foreach (int iCol in arrCol)
{
OutputString = OutputString + "\t" + tb.Rows[iRow][iCol].ToString();
}
fw.WriteLine(OutputString.Trim());
}
fw.Close();
f.Close();