using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;
using System.Web;
using System.IO;
using eName.App.Config;
using System.Data.OleDb;
using System.Data.Odbc;
using ServerHoldUnlock.Info.Table;
namespace ServerHoldUnlock.Libs
{
public class Excels
{
public string CopyExcel()
{
string name = DateTime.Now.ToString("yyyyMMddHHmmss");
string path = string.Format(AppSetting.Path,name);
System.IO.File.Copy(System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Excel/Sample.xls"), System.Web.HttpContext.Current.Server.MapPath(path));
return name;
}
public void DateToExcel(List<ServerHoldUnlockTable> list, string name)
{
string path = string.Format(AppSetting.Path, name);
string strCon = string.Format("Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source ={0};Extended Properties=Excel 8.0", System.Web.HttpContext.Current.Server.MapPath(path));
OleDbConnection myConn = new OleDbConnection(strCon);
try
{
myConn.Open();
for (int i = 0; i < list.Count; i++)
{
string sql = string.Format("INSERT INTO [Sheet1$] VALUES('{0}','{1}')", list[i].Domain, list[i].ICP);
using (OleDbCommand comm = new OleDbCommand(sql, myConn))
{
comm.ExecuteNonQuery();
}
}
myConn.Close();
myConn.Dispose();
}
catch
{
myConn.Close();
myConn.Dispose();
}
}
/// <summary>
/// 從web頁面下載文件
/// </summary>
/// <param name="strFile">要下載文件的絕對路徑</param>
public void DownloadFile(string strFile)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + HttpUtility.UrlEncode(Path.GetFileName(strFile).Trim()) + "\"");
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.WriteFile(strFile);
HttpContext.Current.Response.End();
}
}
}
事先要創(chuàng)建好Excel的模板,程序復(fù)制調(diào)用
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點擊舉報。