C#WINFORM把输入的文本导出excel 因客户需要,他不想直接在EXCEL中操作。因为 字段实在太多。 然后我要用C# winform做一个小软件 功能是 把字段设定好。然后 在里头对号输入。。比如 卡好字段 姓名 年龄 像填表一样 直接把文本都填近去就是了。然后button把输入进去的东西 导出成EXCEL表 我想问的是不经过数据库能实现么?具体怎么实现?是用DataGridView 还是别的什么? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你还真是无孔不入呀,网上很多的http://www.yongfa365.com/Item/DataGridViewToExcel.html public partial class Default3 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Bind(); //窗体加载时向GridView中填充数据 } } PosDataContext pos = new PosDataContext(); //使用Linq to sql进行操作数据库的数据 //自定义向GridView控件填充数据方法 private void Bind() { GridView1.DataSource = pos.Product.ToList(); //填充数据 GridView1.DataKeyNames = new string[] {"Id" }; //设置主键字段 GridView1.DataBind(); //将数据绑定到控件中 } //导出数据事件 protected void btnExcel_Click(object sender, EventArgs e) { Export("application/ms-excel","商品信息报表.xls"); } //自定义导出数据的方法 private void Export(string FileType, string FileName) { Response.Charset = "GB2312"; //设置获取输出数据的类型 Response.ContentEncoding = System.Text.Encoding.UTF7; //将Http头添加到输出流 Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString()); Response.ContentType = FileType; this.EnableViewState = false; StringWriter tw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(tw); GridView1.RenderControl(hw); //将GridView中的内容输出到指定字符串中 Response.Write(tw.ToString()); Response.End(); //将当前面的所有缓冲发送到客户端 } //如果没有下面方法会报错类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内 public override void VerifyRenderingInServerForm(Control control) { //base.VerifyRenderingInServerForm(control); } //将Excel中的数据导入到指定控件中 public DataSet CreateDataSource() { string strCon = "provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("Excel.xls")+"; Extended Properties= Excle 8.0"; OleDbConnection olecon=new OleDbConnection(strCon); OleDbDataAdapter myda=new OleDbDataAdapter("select * from [Sheet1$]",strCon); DataSet myds=new DataSet(); myda.Fill(myds); return myds; } //将数据从Excel导入到指定控件中 protected void btnExcelInfo_Click(object sender, EventArgs e) { GridView1.DataSource= CreateDataSource(); GridView1.DataBind(); } } js 如何获取gridview中选定行中某列的值? 写了一个excel导入程序,IE无法显示网页。 求教 请教:一个关于存储过程的,如何将select的结果变成一个数组? 获取datalist 行的id 简单问题,请回答,关于控制框架里面的滚动条。100分 SOS救命!急需ASP.NET实现视频聊天的源代码! 怎么提取一段指定的HTML代码? 谁能请介绍一个用vb写的.net的例子,能下载的学习。 请问怎么把CheckBox怎么和数据库关联起来? flash游戏网站,如何防止swf文件盗链,谢谢 webform中水晶报表取消登录框问题。。。。。。。。。
http://www.yongfa365.com/Item/DataGridViewToExcel.html
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
Bind(); //窗体加载时向GridView中填充数据
}
}
PosDataContext pos = new PosDataContext(); //使用Linq to sql进行操作数据库的数据
//自定义向GridView控件填充数据方法
private void Bind()
{
GridView1.DataSource = pos.Product.ToList(); //填充数据
GridView1.DataKeyNames = new string[] {"Id" }; //设置主键字段
GridView1.DataBind(); //将数据绑定到控件中
}
//导出数据事件
protected void btnExcel_Click(object sender, EventArgs e)
{
Export("application/ms-excel","商品信息报表.xls");
}
//自定义导出数据的方法
private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312"; //设置获取输出数据的类型
Response.ContentEncoding = System.Text.Encoding.UTF7;
//将Http头添加到输出流
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw); //将GridView中的内容输出到指定字符串中
Response.Write(tw.ToString());
Response.End(); //将当前面的所有缓冲发送到客户端
}
//如果没有下面方法会报错类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
//将Excel中的数据导入到指定控件中
public DataSet CreateDataSource() {
string strCon = "provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("Excel.xls")+"; Extended Properties= Excle 8.0";
OleDbConnection olecon=new OleDbConnection(strCon);
OleDbDataAdapter myda=new OleDbDataAdapter("select * from [Sheet1$]",strCon);
DataSet myds=new DataSet();
myda.Fill(myds);
return myds;
}
//将数据从Excel导入到指定控件中
protected void btnExcelInfo_Click(object sender, EventArgs e)
{
GridView1.DataSource= CreateDataSource();
GridView1.DataBind();
}
}