web中导出EXCEL时,如何显示保存对话框,在线(急!)

hygrom 2004-11-22 09:35:37
web中导出EXCEL时,如何显示保存对话框,在线(急!)
...全文
200 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
nnh 2004-11-22
  • 打赏
  • 举报
回复
<%@ Page Language="VB" AutoEventWireup="True" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Color" %>

<html>
<script runat="server">
dim MyConnection as OleDbConnection
dim cmdSelect as OleDbCommand
dim dtrReader as OleDbDataReader
Sub Page_Load(sender As Object, e As EventArgs)
MyConnection = New OleDbConnection( "Provider=Microsoft.Jet.OLEDB.4.0; Data Source =" + Server.MapPath(".")+"/db/kk.mdb")
If Not IsPostBack Then
BindGrid()
End If
End Sub

Sub BindGrid()
Dim MyCommand As OleDbDataAdapter = new OleDbDataAdapter("select * from [board]", MyConnection)
Dim DS As DataSet = new DataSet()
MyCommand.Fill(DS,"min")
MyDataGrid.DataSource =DS.Tables("min").DefaultView
MyDataGrid.DataBind()
End Sub

Sub Mydatagrid_PageIndexChanged(s as object,e as DataGridPageChangedEventargs)
MyDataGrid.CurrentPageIndex=e.newPageIndex
BindGrid()
End Sub

Sub Button1_Click(sender As Object, e As System.EventArgs)
'-------------------
MyDataGrid.AllowPaging = "false"
BindGrid()
'-------------------
MyDataGrid.SelectedItemStyle.BackColor=Color.white
MyDataGrid.AlternatingItemStyle.BackColor=Color.white
MyDataGrid.ItemStyle.BackColor=Color.white
MyDataGrid.HeaderStyle.BackColor=Color.white
MyDataGrid.HeaderStyle.ForeColor=Color.red
'----------
dim resp as HttpResponse = Page.Response
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312")
Response.ContentType = "application/vnd.ms-excel"
Response.Charset = ""
Me.EnableViewState = False
Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
MyDataGrid.RenderControl(hw)
Response.Write(tw.ToString())
Response.End()
'-------------------
MyDataGrid.AllowPaging = "true"
BindGrid()
End Sub



</script>
<body>
<form runat="server">
<asp:Label id="lblFile" forecolor="red" runat="server"/><BR>
<asp:Button id="button1" onclick="Button1_Click" text="Write To Excle File" runat="server"/>
<BR>
<asp:datagrid id="MyDataGrid"
runat="server"
CellPadding="4"
BackColor="White"
BorderColor="#330033"
BorderWidth="1px"
BorderStyle="None"
AllowPaging="true"
pageSize="10"
OnPageIndexChanged="Mydatagrid_PageIndexChanged"
>
<PagerStyle Mode="NumericPages"
Font-Bold="true"
BackColor="#F2F2F2"
HorizontalAlign="right">
</PagerStyle>
<SelectedItemStyle BackColor="#FFCC66"></SelectedItemStyle>
<AlternatingItemStyle BackColor="#FFCC99"></AlternatingItemStyle>
<ItemStyle BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" HorizontalAlign="Center" ForeColor="#330033" BorderColor="Black" BackColor="#FF9900"></HeaderStyle>
</asp:datagrid>
</form>
</body>
</html>
xinshaw 2004-11-22
  • 打赏
  • 举报
回复
简单点,先保存在服务器端,然后Response.Redirect("xls文件");
8LY8Apollo 2004-11-22
  • 打赏
  • 举报
回复
study
hygrom 2004-11-22
  • 打赏
  • 举报
回复
理解错了,我用的是下面的代码
int rowIndex=1;
string User_ID="3";
Sql.TSqlDB db=new Sql.TSqlDB();
string strSel="select * from tb_sjsj where User_ID='"+User_ID+"' order by id asc";
SqlDataAdapter sqldata=new SqlDataAdapter(strSel,db.SqlConn);
DataSet myset=new DataSet();
sqldata.Fill(myset,"tb_sjsj");
db.SqlConn.Close();
Excel.Application app = new Excel.ApplicationClass();

Object oMissing = System.Reflection.Missing.Value;
Excel.Workbook oBook = app.Workbooks.Add(oMissing);
app.Cells[1,1]="编号";
app.Cells[1,2]="用户编号";
app.Cells[1,3]="序号";
app.Cells[1,4]="井号";
//将所得到的表的列名,赋值给单元格
app.Cells[1,5]="油田";
app.Cells[1,6]="区块";
app.Cells[1,7]="依据地质方案 ";
app.Cells[1,8]=" 施工目的";
app.Cells[1,9]=" 设计日期 ";
app.Cells[1,10]="设计人";
app.Cells[1,11]=" 套管状况 ";
app.Cells[1,12]=" 设计工序1 ";
app.Cells[1,13]="设计工序2";
app.Cells[1,14]="设计工序3";
app.Cells[1,15]="设计工序4 ";
app.Cells[1,16]=" 设计工序5";
app.Cells[1,17]=" 设计工序6 ";
app.Cells[1,18]="设计工序7";
app.Cells[1,19]=" 存在问题 ";
app.Cells[1,20]="分合注 ";
app.Cells[1,21]=" 层数 ";
app.Cells[1,22]=" 最小层间距 ";
app.Cells[1,23]="封隔器型号";
app.Cells[1,24]="封隔器深度";
app.Cells[1,25]="校深 ";
app.Cells[1,26]=" 备注";
foreach(DataRow row in myset.Tables["tb_sjsj"].Rows)
{
rowIndex++;
int colIndex=0;
foreach(DataColumn col in myset.Tables["tb_sjsj"].Columns)
{
colIndex++;
app.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString().Trim();
}
}
app.Visible=true;
oBook.Saved = true;
app.UserControl = false;
adminyao 2004-11-22
  • 打赏
  • 举报
回复
<table width="100%" border=1 cellspacing=1 cellpadding=1 id="table" >
<tr class="tr" align="center">
<td>time</td>
<td>name</td>
<td>size</td>
<td>age</td>
<td>address</td>
</tr>
<tr class="td" >
<td>2004/11/22</td>
<td>abc</td>
<td>ee</td>
<td>dfasd</td>
<td>xxx</td>
</tr>
</table>


<script language="javascript">
function exportExcel(atblData){
if (typeof(EXPORT_OBJECT)!="object"){
document.body.insertAdjacentHTML("afterBegin",
"<OBJECT style='display:none' classid=clsid:0002E510-0000-0000-C000-000000000046 id=EXPORT_OBJECT></Object>");
}
with (EXPORT_OBJECT){
DataType = "HTMLData";
HTMLData =atblData.outerHTML;
try{
("e:\\JSP\\abc.xls", 1);
ActiveSheet.clear();
}
catch (e){ }
}
}

</script>
shevins 2004-11-22
  • 打赏
  • 举报
回复
如果你的网页的meta设置是gb2312,那么charset最好设置成utf-8,和contentencoding一样。
如果你有checkbox,选择导出的话,那么在判断时,!checked的项设置dataGrid1.items[i].style["display"] = "none",以防出现乱码
goody9807 2004-11-22
  • 打赏
  • 举报
回复
string path = Server.MapPath(this.xlfile.Text+".xls");

System.IO.FileInfo file = new System.IO.FileInfo(path);
Response.Clear();
Response.Charset="GB2312";
Response.ContentEncoding=System.Text.Encoding.UTF8;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
Response.AddHeader("Content-Length", file.Length.ToString());

// 指定返回的是一个不能被客户端读取的流,必须被下载
Response.ContentType = "application/ms-excel";

// 把文件流发送到客户端
Response.WriteFile(file.FullName);
// 停止页面的执行

Response.End();

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧