我后台上传了一个Excel文档,前台需要在网页的固定位置将这个Excel文档的内容显示出来(网页上还有其它内容,Excel在网页上只占一小块地方),aps.net(C#)该怎么写?
解决方案 »
- xp系统安装iis的问题?急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急
- javascript获得对象问题
- “/asp.net”应用程序中的服务器错误。(我的asp.net程序无法调试)求救!!!
- XML的新闻系统的困惑
- 如何设置页面为uncode字符集
- 多个文件同时上传的问题!~!!!!
- 各位大侠快进来呀,关于petshop的问题,急S~~~~~~~~~~~~~~~~~~~~
- 谁能帮我根治这个讨厌的VS.NET的BUG???????????????
- 请思归详细的详细地解释一下里面的函数的用处,谢谢
- 关于MultiPage的使用
- access数据删除问题,请高手指教
- 讨论贴:对DataTable里的列进行混合运算后,得到新的列,然后返回这个DataTable,供DataGrid使用.
System.data.OleDb.OleDbConnection myCnn=New OleDbConnection(STR_CNN);
myCnn.Open();
....
Do want you do~~~~~
public void CreateExcel(DataSet ds,string FileName)
{
HttpResponse resp;
resp = Page.Response;
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
string colHeaders= "", ls_item="";
int i=0;
//定义表对象与行对像,同时用DataSet对其值进行初始化
DataTable dt=ds.Tables[0];
DataRow[] myRow=dt.Select("");
//取得数据表各列标题,各标题之间以\t分割,最后一个列标题后加回车符
for(i=0;i<dt.Columns.Count-1;i++)
colHeaders+=dt.Columns[i].Caption.ToString()+"\t";
colHeaders +=dt.Columns[i].Caption.ToString() +"\n";
//向HTTP输出流中写入取得的数据信息
resp.Write(colHeaders);
//逐行处理数据
foreach(DataRow row in myRow)
{
//在当前行中,逐列获得数据,数据之间以\t分割,结束时加回车符\n
for(i=0;i<dt.Columns.Count-1;i++)
ls_item +=row[i].ToString() + "\t";
ls_item += row[i].ToString() +"\n";
//当前行数据写入HTTP输出流,并且置空ls_item以便下行数据
resp.Write(ls_item);
ls_item="";
} //写缓冲区中的数据到HTTP头文件中
resp.End();
}
//通过DataGrid导出
public void ToExcel(System.Web.UI.Control ctl)
{
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls");
HttpContext.Current.Response.Charset ="UTF-8";
HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType ="application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
ctl.Page.EnableViewState =false;
System.IO.StringWriter tw = new System.IO.StringWriter() ;
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
http://azamsharp.net/DataGridExport.htm
1.将Excel看成一中OleDb数据源,用ADO.NET操作
http://dotnet.aspx.cc/ShowDetail.aspx?id=C673E2CD-3F1E-4919-8CE0-D69B894A0599
2.用一些第三方的操作Excel 的控件来实现。比如Office自带的com组建包(叫什么名字记不太清楚了)
直接用excel做出来,导出保存为。htm文件就行了
if (.FileName.ToUpper().EndsWith(".XLS"))ext = ".XLS";
else return;
if (控件.FileName.ToUpper().EndsWith(".XLS"))ext = ".XLS";
else return;
可以介绍一下那个第三方控件吗,或给我提供一些参考资料。。谢谢
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +"Data Source="+ Path +";"+"Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
DataSet ds = null;
strExcel="select * from [sheet1$]";
myCommand = new OleDbDataAdapter(strExcel, strConn);
ds = new DataSet();
myCommand.Fill(ds,"table1");
return ds;
}
/// <summary>
/// 写入Excel文档
/// </summary>
/// <param name="Path">文件名称</param>
public bool SaveFP2toExcel(string Path)
{
try
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +"Data Source="+ Path +";"+"Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
System.Data.OleDb.OleDbCommand cmd=new OleDbCommand ();
cmd.Connection =conn;
//cmd.CommandText ="UPDATE [sheet1$] SET 姓名='2005-01-01' WHERE 工号='日期'";
//cmd.ExecuteNonQuery ();
for(int i=0;i<fp2.Sheets [0].RowCount -1;i++)
{
if(fp2.Sheets [0].Cells[i,0].Text!="")
{
cmd.CommandText ="INSERT INTO [sheet1$] (工号,姓名,部门,职务,日期,时间) VALUES('"+fp2.Sheets [0].Cells[i,0].Text+ "','"+
fp2.Sheets [0].Cells[i,1].Text+"','"+fp2.Sheets [0].Cells[i,2].Text+"','"+fp2.Sheets [0].Cells[i,3].Text+
"','"+fp2.Sheets [0].Cells[i,4].Text+"','"+fp2.Sheets [0].Cells[i,5].Text+"')";
cmd.ExecuteNonQuery ();
}
}
conn.Close ();
return true;
}
catch(System.Data.OleDb.OleDbException ex)
{
System.Diagnostics.Debug.WriteLine ("写入Excel发生错误:"+ex.Message );
}
return false;
}
office web control(全称好象是这样)
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<table cellSpacing="0" cellPadding="0" width="100%" border="0">
<tr>
<td></td>
</tr>
<tr>
<td>请选择Excel表格:
</td>
</tr>
<tr>
<td><input id="file_excel" style="WIDTH: 440px; HEIGHT: 22px" type="file" size="54" runat="server">
</td>
</tr>
<tr>
<td style="HEIGHT: 1px" height="1"></td>
</tr>
<tr>
<td><asp:button id="Button1" runat="server" Width="50px" BorderStyle="Groove" Text="显示"></asp:button></td>
</tr>
<tr>
<td height="3"></td>
</tr>
<tr>
<td align="center"><asp:label id="lbl_title" runat="server" Width="206px"></asp:label></td>
</tr>
<tr>
<td align="center"><asp:datagrid id="DataGrid1" runat="server" Width="452px"></asp:datagrid></td>
</tr>
</table>
</form>
</body>
//后台
private void Button1_Click(object sender, System.EventArgs e)
{ //完整路径
string source=file_excel.Value;
string sqlconn="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+source+";Extended Properties=Excel 8.0";
string sql = "SELECT * FROM [Sheet1$]";
System.Data.OleDb.OleDbCommand oldcom = new System.Data.OleDb.OleDbCommand(sql,new OleDbConnection(sqlconn));
System.Data.OleDb.OleDbDataAdapter oleda = new System.Data.OleDb.OleDbDataAdapter(oldcom);
System.Data.DataSet ds = new DataSet();
oleda.Fill(ds,"[Sheet1$]");
lbl_title.Text=ds.Tables[0].TableName;
DataGrid1.DataSource=ds;
DataGrid1.DataMember="[Sheet1$]";
DataGrid1.DataBind();
}
<input type="textbox" value= "text">
<br>
<iframe src="test.xls" width=400 height=300></iframe>
<br>
<input type=submit>
这样有一个问题就是会提示用户保存 excel,点击打开后就可以在网页的局部位置打开excel了