using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using Kevin.Lib; using Kevin.Model; using Kevin.IBLL; using Kevin.BLLFactory; using System.Collections.Generic; using System.Web.UI.HtmlControls;public partial class Admin_Page_OrderManage_OrderOutPrint : System.Web.UI.Page {
foreach(MODEL model in LstModel)
{
StringBuilder sql = new StringBuilder();
sql.Append("insert into [sheet1$] values(");
sql.AppendFormat("'{0}',", MODEL.No.Replace("'","''"));
sql.AppendFormat("'{0}',", MODEL.Name.Replace("'", "''"));
sql.AppendFormat("'{0}',", MODEL.UserName.Replace("'", "''"));
sql.AppendFormat("'{0}',", MODEL.Password.Replace("'", "''"));
sql.AppendFormat("'{0}',", MODEL.Ip);
sql.AppendFormat("'{0}',", MODEL.Port);
sql.AppendFormat("'{0}',", MODEL.ChannelCount);
sql.AppendFormat("'{0}',", MODEL.ConnectMatrixNo.Replace("'", "''"));
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelFilePath(文件路径) + ";Extended Properties=Excel 8.0;";
OleDbConnection connection = new OleDbConnection(connectionString);
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection ;
command.CommandText = sql;
command.ExecuteNonQuery();
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Kevin.Lib;
using Kevin.Model;
using Kevin.IBLL;
using Kevin.BLLFactory;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;public partial class Admin_Page_OrderManage_OrderOutPrint : System.Web.UI.Page
{
protected void Button_Click(object sender, CommandEventArgs e)
{
switch (e.CommandName)
{
case "save":
switch (listType.SelectedValue)
{
case "excel":
OutPut("attachment;filename=out.xls", "application/ms-excel");
break;
case "word":
OutPut("attachment;filename=out.doc", "application/ms-word");
break;
}
break;
case "open":
switch (listType.SelectedValue)
{
case "excel":
OutPut("online;filename=out.xls", "application/ms-excel");
break;
case "word":
OutPut("online;filename=out.doc", "application/ms-word");
break;
}
break;
}
}
private void OutPut(string fileType, string strType)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
//追加文件表头值
Response.AppendHeader("Content-Disposition", fileType);
Response.ContentType = strType;
this.EnableViewState = false;
//创建HtmlTextWriter
System.IO.StringWriter swOut = new System.IO.StringWriter();
HtmlTextWriter hTw = new HtmlTextWriter(swOut);
//将服务器控件的内容输出到所提供的 HtmlTextWriter 对象中;
myGW.RenderControl(hTw);
Response.Write(swOut.ToString());
Response.End();
}
}
导出到word或Exsl可以这样做 不个的定义好Exsl 中的字段与 数据库中字段一样