问题一:我想在前台用javascript获取screen.width,然后在后台用Session或者服务器控件保存值,怎么实现???希望哪为高手前辈能给详细的实现代码,谢谢!
问题二:将DataGrid数据导出为Excel时,有两个选择,一个是打开,一个是保存,我想让它直接打开,如何实现???
问题二:将DataGrid数据导出为Excel时,有两个选择,一个是打开,一个是保存,我想让它直接打开,如何实现???
解决方案 »
- (急)ajax显示日期的问题,HELP………………
- 怎样防止别人下载网站文件
- 求复选框式的树形菜单调用方法源码
- asp.net js自动增行以后,在后台如何取到值
- 送分的菜问题Calendar1控件选定了日期其他控件怎么获取他的值
- 求助,正规表达式过滤复杂HTML代码问题
- 可否在vs2005中开发asp.net1.1的项目?
- 关于HttpContext.Current.User的问题,一直不明白
- 求教,关于ssl的几个问题!up有分!
- 求教(急):对Excel或word操作时总是报同一个异常??
- Web开发中,如何在DataGrid中显示数据的合计项(像一些报表一样),DataGrid控件有没有这一功能?
- 问一个DataList中绑定checkbox,获得相应值的问题,请指教~~~~~~~~~~
<input type="hidden" value="" runat="server" id="hidScreenWidth">
<script language="javascript">
window.onload = function(){
if(document.all.hidScreenWidth.value = "") {
document.all.hidScreenWidth.value = window.screen.width;
document.forms[0].submit();
}
}
</script>
服务端直接访问: string strWidth = this.hidScreenWidth.Value;2. 这得看客户端的IE安全设置,Xp SP2的电脑肯定会弹出询问对话框。
content-type=inline;application=vnd.ms-excel;filename=test.xls;
不知道我是不是测试错误,我测试得到的值总为"".测试完整代码如下:
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication8.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script language="javascript">
window.onload = function()
{
if(document.all.hidScreenWidth.value = "")
{
document.all.hidScreenWidth.value = window.screen.width;
document.forms[0].submit();
}
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<input type="hidden" runat="server" id="hidScreenWidth" NAME="hidScreenWidth">
</form>
</body>
</HTML>public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputHidden hidScreenWidth;
private void Page_Load(object sender, System.EventArgs e)
{
string strWidth = this.hidScreenWidth.Value;
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
}
后台方法,生成excel
public static void ExportToExcel( System.Data.DataTable dt,string filename)
{
//生成Excel操作相关对象
Excel.Application xlApp;
Excel.Workbook xlBook;
Excel.Worksheet xlSheet;
xlApp = new Application();
xlBook = xlApp.Workbooks.Add(1);
xlSheet =(Excel.Worksheet)xlBook.Worksheets[1] ;
//xlSheet.Range("A1:B1").Merge(0) '合并单元格
//xlSheet.Cells(1, 1) = "员工资料信息:"
//赋标题(Excel文件中的标题)
int rowIndex = 2;
int colIndex = 0;
DataColumn Col;
DataRow Row;
foreach (DataColumn tempLoopVar_Col in dt.Columns)
{
Col = tempLoopVar_Col;
colIndex++;
xlApp.Cells[1, colIndex] = Col.Caption;
}
//将表dt的所有行写入xlApp对象(Excel文件中的内容)
foreach (DataRow tempLoopVar_Row in dt.Rows)
{
Row = tempLoopVar_Row;
colIndex = 0;
foreach (DataColumn tempLoopVar_Col in dt.Columns)
{
Col = tempLoopVar_Col;
colIndex++;
if(colIndex==3)
xlApp.Cells[rowIndex, colIndex] = "'"+Row[Col.ColumnName].ToString();
else
xlApp.Cells[rowIndex, colIndex] = Row[Col.ColumnName].ToString();
}
rowIndex++;
}
xlSheet.Application.Visible = true; //置为可见
//建立一个专门存放Excel文件的目录
if (Directory.Exists(AppConfig.ExportUrl) == false)
{
Directory.CreateDirectory(AppConfig.ExportUrl);
}
//删除服务端临时文件: download.xls
// if (File.Exists(@AppConfig.ExportUrl+"download.xls") == true)
// {
// File.Delete(@AppConfig.ExportUrl+"download.xls");
// }
//在服务端保存download.xls
//xlSheet.SaveAs(Page.Server.MapPath(".") + "\\ExcelFolder\\download.xls");
xlApp.ActiveWorkbook.SaveAs(filename, Excel.XlFileFormat.xlExcel4Workbook, Type.Missing, Type.Missing, Type.Missing, Type.Missing,Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing,Type.Missing, Type.Missing, Type.Missing, Type.Missing);
xlApp.Quit();
xlApp= null ;
//杀死Excel进程
//Process myproc = new Process();
Process proc;
Process[] procs = Process.GetProcessesByName("EXCEL"); //得到所有打开的进程
try
{
foreach (Process tempLoopVar_proc in procs)
{
proc = tempLoopVar_proc;
if (! proc.CloseMainWindow())
{
proc.Kill();
}
}
}
catch
{}
}前台ui输出到客户端
again:
try
{
//输出到客户端()
if (File.Exists(filename))
{
FileInfo TargetFile = new FileInfo(filename);
//清除缓冲区流中的所有内容输出.
Page.Response.Clear();
//向输出流添加HTTP头 [指定下载/保存 对话框的文件名]
Page.Response.AppendHeader("Content-Disposition", "attachment; filename=" + Page.Server.UrlEncode(TargetFile.Name));
//向输出流添加HTTP头 [指定文件的长度,这样下载文件就会显示正确的进度
Page.Response.AppendHeader("Content-Length", TargetFile.Length.ToString());
//表明输出的HTTP为流[stream],因此客户端只能下载.
Page.Response.ContentType = "application/octet-stream";
//发送文件流到客户端.
Page.Response.WriteFile(TargetFile.FullName);
//停止执行当前页
//Response.Clear();
Page.Response.End();
}
}
catch
{
Thread.Sleep(10);
goto again;
}
}
不过呢,我还是觉得有点长,看不明白具体完成我的要求的那句代码,不知道是不是下面这一行:
Page.Response.AppendHeader("Content-Length", TargetFile.Length.ToString());
二楼对于第一个问题的回答,我也早知道了,可是测试了N次,检查得到的隐藏控件的值总为空,我就是想知道,为什么会为空,错在哪?
第二个问题我也知道,在XP SP2补丁下,客户端会进行阻止,我就是想知道解决的方法,而不是一句结果