高手请进, 关于自动读取数据源并生成EXCEL表 其实关于程序自动启动完全可以用WINDOWS中的计划任务指定时间。但。NET程序中如何设计一个指取完数据自动关闭程序的功能。也就是说。每次启动程序之后就进行读取。这后自动关闭。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 为了方便高手指导问题。我将程序指定读取URL的地址公布如下。http://www.sxsqxt.com/yh2/ydnr1.htm 首先获取网页内容,即网页的Html内容.代码示例如下: private string GetHtmlFromURL(string sAddress) { System.Text.StringBuilder sbHtml=new System.Text.StringBuilder(); //Disable button btnGetHtml.Enabled=false; HttpWebRequest hwReq=(HttpWebRequest)WebRequest.Create(sAddress); HttpWebResponse hwResponse=(HttpWebResponse)hwReq.GetResponse(); Stream stream=hwResponse.GetResponseStream(); StreamReader reader=new StreamReader(stream,System.Text.Encoding.GetEncoding("GB2312"));// char[] read = new char[256]; // Reads 256 characters at a time. int count = reader.Read( read, 0, 256 ); while (count > 0) { // Dumps the 256 characters on a string and displays the string to the console. string str = new string(read, 0, count); sbHtml.Append(str); count =reader.Read(read,0,256); } // Releases the resources hwResponse.Close(); stream.Close(); reader.Close(); return sbHtml.ToString(); }然后,将string保存为一个文件,注意~文件后缀名为xls(即Excel)的文件名.然后直接用Excel就可以打开.//呵呵~这是因为Excel会自动读取Htmal格式.如果你希望将其保存为真正的xls格式的文件,那就在后台打开一个Excel,读取这个文件然后将它另存为一个xls格式(注意不光是文件后缀名)的文件.关于怎么调用Excel打开一个文件和另存一个文件.可参考相关文章.这里不再重复. 如何生产一个新的Excel文件以写入数据并保存1. 首先,产生一个文件夹来存放Excel文件private string genReportPath() { try { string _path="..//ReportDoc//DRI"; if (!Directory.Exists(Server.MapPath(_path))) { Directory.CreateDirectory(Server.MapPath(_path)); } return _path; } catch(Exception er) { throw er; } }2.确定Excel文件string reportpath=this.genReportPath()+"//"+_reportname+".xls"; 3.生成Excel文件this.genExcel(ds_DRI,Server.MapPath(reportpath).ToString());4.生成函数(需要一个DataSet,还需要一个路径)private void genExcel(DataSet ds,string ReportPath) { try { //删除重复的文件; if (File.Exists(ReportPath)) { File.Delete(ReportPath); } FileStream fsobj=new FileStream(ReportPath,System.IO.FileMode.Create,FileAccess.ReadWrite); //生成一个文件流 StreamWrite _sw=new StreamWriter(fsobj,System.Text.UnicodeEncoding.Unicode);//生成一个写入器//开始写入DataTable dt=ds.Tables[0]; for(int j=0;j<countMRD;j++) { _sw.Write(dt.Rows[j][0].ToString().Trim()+"\t"); _sw.Write(dt.Rows[j][1].ToString().Trim()+"\t"); _sw.Write(dt.Rows[j][2].ToString().Trim()+"\t"); _sw.Write(dt.Rows[j][3].ToString().Trim()+"\t"); _sw.Write(dt.Rows[j][4].ToString().Trim()+"\t"); _sw.Write(dt.Rows[j][5].ToString().Trim()+"\t"); _sw.Write(dt.Rows[j][6].ToString().Trim()+"\t"); _sw.Write(dt.Rows[j][7].ToString().Trim()+"\t"); _sw.Write(dt.Rows[j][8].ToString().Trim()+"\t"); _sw.Write(dt.Rows[j][9].ToString().Trim()+"\t");}_sw.close();fsobj.close(); } catch(Exception er) { throw er; } 超级终端-AT指令怎样清屏? 求一个Asp.Net控件 C# 高斯列主元消去法 一个图形问题 求根据输入汉语得到拼音码的C#程序(不要带数据库的) 关于线程创建窗体的问题 wpf3d漫游效果 Why do java develops wear glasses? Because they don't see sharp(C#)! 求教,如何获取文件的“修改日期”? 怎么样在c#中从dll中获取类的信息? 怎么实现在treeview里单击父节点打开子节点(web)! 如何对红外设备进行操作?
我将程序指定读取URL的地址公布如下。http://www.sxsqxt.com/yh2/ydnr1.htm
代码示例如下:
private string GetHtmlFromURL(string sAddress)
{
System.Text.StringBuilder sbHtml=new System.Text.StringBuilder();
//Disable button
btnGetHtml.Enabled=false; HttpWebRequest hwReq=(HttpWebRequest)WebRequest.Create(sAddress);
HttpWebResponse hwResponse=(HttpWebResponse)hwReq.GetResponse();
Stream stream=hwResponse.GetResponseStream();
StreamReader reader=new StreamReader(stream,System.Text.Encoding.GetEncoding("GB2312"));// char[] read = new char[256];
// Reads 256 characters at a time.
int count = reader.Read( read, 0, 256 );
while (count > 0)
{
// Dumps the 256 characters on a string and displays the string to the console.
string str = new string(read, 0, count);
sbHtml.Append(str);
count =reader.Read(read,0,256);
}
// Releases the resources
hwResponse.Close();
stream.Close();
reader.Close(); return sbHtml.ToString();
}然后,将string保存为一个文件,注意~文件后缀名为xls(即Excel)的文件名.
然后直接用Excel就可以打开.//呵呵~这是因为Excel会自动读取Htmal格式.如果你希望将其保存为真正的xls格式的文件,那就在后台打开一个Excel,读取这个文件
然后将它另存为一个xls格式(注意不光是文件后缀名)的文件.
关于怎么调用Excel打开一个文件和另存一个文件.可参考相关文章.这里不再重复.