求c# 对excel读取写入的代码 带注释最好
2013-11-17
展开全部
string style = @"<style> .text { mso-number-format:\@; } </script> ";
//清空
response.ClearContent();
//导出格式
response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
response.ContentType = "application/ms-excel";
response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
//创建流
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gv.RenderControl(htw);
response.Write(style);
response.Write(sw.ToString());
response.End();
//必须的方法
public override void VerifyRenderingInServerForm(Control control)
{
}
以上是导出
~~~~~~~~~
以下是导入
private DataSet CreateDataSource()
{
string strCon;
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("excel.xls") + "; Extended Properties=Excel 8.0;";
OleDbConnection olecon = new OleDbConnection(strCon);
OleDbDataAdapter myda = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strCon);
DataSet myds = new DataSet();
myda.Fill(myds);
return myds;
}
protected void Button1_Click(object sender, EventArgs e)
{
GridView1.DataSource = CreateDataSource();
GridView1.DataBind();
}
好吧,是不是很简单呢?
//清空
response.ClearContent();
//导出格式
response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
response.ContentType = "application/ms-excel";
response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
//创建流
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gv.RenderControl(htw);
response.Write(style);
response.Write(sw.ToString());
response.End();
//必须的方法
public override void VerifyRenderingInServerForm(Control control)
{
}
以上是导出
~~~~~~~~~
以下是导入
private DataSet CreateDataSource()
{
string strCon;
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("excel.xls") + "; Extended Properties=Excel 8.0;";
OleDbConnection olecon = new OleDbConnection(strCon);
OleDbDataAdapter myda = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strCon);
DataSet myds = new DataSet();
myda.Fill(myds);
return myds;
}
protected void Button1_Click(object sender, EventArgs e)
{
GridView1.DataSource = CreateDataSource();
GridView1.DataBind();
}
好吧,是不是很简单呢?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-11-17
展开全部
加我给你一个完整的操作类 懒得贴代码了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询