做了一个.XLS模板,里面有统计函数和图表.在C#中怎么引用? 就是说,我要加载这个.xls的模板,然后把统计图表取到.请问怎么实现的?比如 Sheet1是需要导入的原始数据.sheet2某像统计的数据和图表. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 C#窗体中嵌入excel的模板string strConn = "Data Source=.;Initial Catalog=SuieDB;Integrated Security=True"; protected void Page_Load(object sender, EventArgs e) { SqlConnection cn = new SqlConnection(strConn); cn.Open(); SqlDataAdapter sda = new SqlDataAdapter("select * from Softs", cn); DataSet ds = new DataSet(); sda.Fill(ds, "Softs"); this.GridView1.DataSource = ds.Tables["Softs"]; this.GridView1.DataSource = ds.Tables[0].DefaultView; this.GridView1.DataBind(); } public DataSet ExecleDs(string filenameurl, string table) { string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + filenameurl + ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'"; OleDbConnection conn = new OleDbConnection(strConn); conn.Open(); DataSet ds = new DataSet();; OleDbDataAdapter odda = new OleDbDataAdapter("select * from [Sheet1$]", conn); //Excel表 odda.Fill(ds, table); return ds; } protected void Button1_Click(object sender, EventArgs e) { if (FileUpload1.HasFile == false)//HasFile用来检查FileUpload是否有指定文件 { Response.Write("<script>alert('请您选择Excel文件')</script> "); return;//当无文件时,返回 } string IsXls = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();//System.IO.Path.GetExtension获得文件的扩展名 if (IsXls != ".xls") { Response.Write("<script>alert('只可以选择Excel文件')</script>"); return;//当选择的不是Excel文件时,返回 } SqlConnection cn = new SqlConnection(strConn); cn.Open(); string filename = DateTime.Now.ToString("yyyymmddhhMMss") + FileUpload1.FileName; //获取Execle文件名 DateTime日期函数 string savePath = Server.MapPath(("~\\upfiles\\") + filename);//Server.MapPath 获得虚拟服务器相对路径 FileUpload1.SaveAs(savePath); //SaveAs 将上传的文件内容保存在服务器上 DataSet ds = ExecleDs(savePath, filename); //调用自定义方法 DataRow[] dr = ds.Tables[0].Select(); //定义一个DataRow数组 int rowsnum = ds.Tables[0].Rows.Count; if (rowsnum == 0) { Response.Write("<script>alert('Excel表为空表,无数据!')</script>"); //当Excel表为空时,对用户进行提示 } else { for (int i = 0; i < dr.Length; i++) { string author_Code = dr[i]["软件号"].ToString();//软件号 excel列名【名称不能变,否则就会出错】 string serial_Code = dr[i]["授权码"].ToString();//授权码 列名 以下类似 string sqlcheck = "select count(*) from Softs where AuthorCode='" + author_Code + "'And SerialCode='" + serial_Code + "'"; //检查用户是否存在 SqlCommand sqlcmd = new SqlCommand(sqlcheck, cn); int count = Convert.ToInt32(sqlcmd.ExecuteScalar()); if (count < 1) { string insertstr = "insert into Softs (AuthorCode,SerialCode) values('" + author_Code + "','" + serial_Code + "')"; SqlCommand cmd = new SqlCommand(insertstr, cn); try { cmd.ExecuteNonQuery(); } catch (MembershipCreateUserException ex) //捕捉异常 { Response.Write("<script>alert('导入内容:" + ex.Message + "')</script>"); } } else { Response.Write("<script>alert('内容重复!禁止导入');location='default.aspx'</script></script> "); continue; } } Response.Write("<script>alert('Excle表导入成功!');location='default.aspx'</script>"); } cn.Close(); }这是把excel存在数据库里,在显示在页面上的,看看有帮助吗? 前台页面中使用js调用后台C#方法相关知识请教...... 急 文章分页 运行asp.net页面老是出现连接的用户过多。 看了MSPetShop后的一些困惑 如何捕获后台添加的客户端控件? 可不可以把panel显示在浏览器指定的地方的啊? 请教 asp.net 象论坛一样显示记录的方法!(在线等) 如何按着最新的时间显示 用WebClient或HttpWebRequest 如何上传byte数据文件? 用asp怎样实现跳转 获取网页文本,请各位高手指点!!! asp.net 与socket通讯的问题在线等!!!! 使用TPL时如何管理线程池
string strConn = "Data Source=.;Initial Catalog=SuieDB;Integrated Security=True"; protected void Page_Load(object sender, EventArgs e)
{ SqlConnection cn = new SqlConnection(strConn);
cn.Open();
SqlDataAdapter sda = new SqlDataAdapter("select * from Softs", cn);
DataSet ds = new DataSet();
sda.Fill(ds, "Softs");
this.GridView1.DataSource = ds.Tables["Softs"]; this.GridView1.DataSource = ds.Tables[0].DefaultView;
this.GridView1.DataBind(); } public DataSet ExecleDs(string filenameurl, string table)
{
string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + filenameurl + ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
DataSet ds = new DataSet();;
OleDbDataAdapter odda = new OleDbDataAdapter("select * from [Sheet1$]", conn); //Excel表
odda.Fill(ds, table);
return ds;
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile == false)//HasFile用来检查FileUpload是否有指定文件
{
Response.Write("<script>alert('请您选择Excel文件')</script> ");
return;//当无文件时,返回
}
string IsXls = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();//System.IO.Path.GetExtension获得文件的扩展名
if (IsXls != ".xls")
{
Response.Write("<script>alert('只可以选择Excel文件')</script>");
return;//当选择的不是Excel文件时,返回
}
SqlConnection cn = new SqlConnection(strConn);
cn.Open();
string filename = DateTime.Now.ToString("yyyymmddhhMMss") + FileUpload1.FileName; //获取Execle文件名 DateTime日期函数
string savePath = Server.MapPath(("~\\upfiles\\") + filename);//Server.MapPath 获得虚拟服务器相对路径
FileUpload1.SaveAs(savePath); //SaveAs 将上传的文件内容保存在服务器上
DataSet ds = ExecleDs(savePath, filename); //调用自定义方法
DataRow[] dr = ds.Tables[0].Select(); //定义一个DataRow数组
int rowsnum = ds.Tables[0].Rows.Count;
if (rowsnum == 0)
{
Response.Write("<script>alert('Excel表为空表,无数据!')</script>"); //当Excel表为空时,对用户进行提示
}
else
{
for (int i = 0; i < dr.Length; i++)
{
string author_Code = dr[i]["软件号"].ToString();//软件号 excel列名【名称不能变,否则就会出错】
string serial_Code = dr[i]["授权码"].ToString();//授权码 列名 以下类似
string sqlcheck = "select count(*) from Softs where AuthorCode='" + author_Code + "'And SerialCode='" + serial_Code + "'"; //检查用户是否存在
SqlCommand sqlcmd = new SqlCommand(sqlcheck, cn);
int count = Convert.ToInt32(sqlcmd.ExecuteScalar());
if (count < 1)
{
string insertstr = "insert into Softs (AuthorCode,SerialCode) values('" + author_Code + "','" + serial_Code + "')"; SqlCommand cmd = new SqlCommand(insertstr, cn);
try
{
cmd.ExecuteNonQuery();
}
catch (MembershipCreateUserException ex) //捕捉异常
{
Response.Write("<script>alert('导入内容:" + ex.Message + "')</script>");
}
}
else
{
Response.Write("<script>alert('内容重复!禁止导入');location='default.aspx'</script></script> ");
continue;
}
}
Response.Write("<script>alert('Excle表导入成功!');location='default.aspx'</script>");
} cn.Close();
}
这是把excel存在数据库里,在显示在页面上的,看看有帮助吗?