如何将表中数据导入Excel中?

Jinniu 2003-08-12 10:24:07
如何将表中数据导入Excel中?
...全文
21 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
veaven 2003-08-12
  • 打赏
  • 举报
回复
关于导出数据到excel,你可以参看上面成功的例子,分析一下你的代码是什么原因导致的错误。
veaven 2003-08-12
  • 打赏
  • 举报
回复
//You use these variables throughout the application.
string fileExcel, filePath, fileName, strLine, sql;
FileStream objFileStream;
StreamWriter objStreamWriter;
Random nRandom = new Random(DateTime.Now.Millisecond);
SqlConnection cnn = new SqlConnection("data source=(local);initial catalog=chinapackage;user id=sa;password=;");

//Create a random file name.
fileExcel = "t" + nRandom.Next().ToString() + ".xls";

//Set a virtual folder to save the file.
//Make sure to change the application name to match your folder.
filePath = Server.MapPath("\\StartExcel");
fileName = filePath + "\\" + fileExcel;

//Use FileSystemObject to create the .xls file.
objFileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
objStreamWriter = new StreamWriter(objFileStream, System.Text.Encoding.Unicode);

//Use a DataReader object to connect to the Pubs database.
cnn.Open();
//sql = "select au_id,au_lName,au_fname,phone,address,city,state,zip,contract from authors";
sql = "select * from tbdownload";
SqlCommand cmd = new SqlCommand(sql, cnn);
SqlDataReader dr;
dr = cmd.ExecuteReader();

//Initialize the string that is used to build the file.
strLine = "";

//Enumerate the field names and the records that are used to build
//the file.
for (int i = 0; i <= dr.FieldCount-1; i++)
{
strLine = strLine + dr.GetName(i).ToString() + Convert.ToChar(9);
}

//Write the field name information to the file.
objStreamWriter.WriteLine(strLine);

//Reinitialize the string for data.
strLine = "";

//Enumerate the database that is used to populate the file.
while (dr.Read())
{
for (int i = 0; i <= dr.FieldCount-1; i++)
{
strLine = strLine + dr.GetValue(i).ToString() + Convert.ToChar(9);
}
objStreamWriter.WriteLine(strLine);
strLine="";
}

//Clean up.
dr.Close();
cnn.Close();
objStreamWriter.Close();
objFileStream.Close();

//Include a link to the Excel file.
HyperLink1.Text="Open Excel";
HyperLink1.NavigateUrl=fileExcel;

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧