using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data.OleDb;
using System.Data;
namespace CardProject.admin.resource
{
public partial class test2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void daochu()
{
string filePath = Server.MapPath("~/" + Guid.NewGuid().ToString() + ".xls");
File.Copy(Server.MapPath("~/Book1.xls"), filePath);
// 使用OleDb驱动程序连接到副本
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=Excel 8.0;");
using (conn)
{
conn.Open();
// 增加记录
OleDbCommand cmd = new OleDbCommand("INSERT INTO [Sheet1$]([年龄], [姓名], [生日]) VALUES(@Id, @Name, @Birthday)", conn);
cmd.Parameters.AddWithValue("@Id", "1");
cmd.Parameters.AddWithValue("@Name", "Hsu Yencheng");
cmd.Parameters.AddWithValue("@Birthday", "1981-10-13");
cmd.ExecuteNonQuery();
}
// 输出副本的二进制字节流
Response.ContentType = "application/ms-excel";
Response.AppendHeader("Content-Disposition", "attachment;filename=info.xls");
Response.BinaryWrite(File.ReadAllBytes(filePath));
// 删除副本
//File.Delete(filePath);
#region
// // 导出数据:
// //string filePath = Server.MapPath("~/info.xls");
// //OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM [Sheet1$]","Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=Excel 8.0");
// //DataTable dt = new DataTable();
// //da.Fill(dt);
// public DataTable GetDataFromExcel(string filepath)
// {
// try
// {
// string ext = Path.GetExtension(filepath).ToLower();
// string strConn;
// if (ext== ".xls")
// {
// strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;";
// }
// else if (ext== ".xlsx")
// {
// strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties='Excel 12.0 Xml;HDR=YES' ";
// }
// else
// {
// throw new Exception("filepath输入错误!");
// }
// OleDbDataAdapter da = new OleDbDataAdapter("SELECT 工号,姓名,考核结果 FROM [Sheet1$]", strConn);
// DataTable dt = new DataTable();
// da.Fill(dt);
// return dt;
// }
// catch
// {
// throw;
// }
#endregion
}
protected void Button1_Click(object sender, EventArgs e)
{
daochu();
}
}
}