打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
List导入Excel文件
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();
  }
 }
}
事先要创建好Excel的模板,程序复制调用
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Excel导入DataSet - 生 - 博客园
读取EXCEL的数据到datagridview
C#操作Excel(读取)
C# 将数据导出到Excel汇总
asp.net常用javascript封装代码
网站安全性:C#防SQL注入代码的实现方法 - 51CTO.COM
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服