文件下载问题急!!!!!

zu274568031 2008-05-30 03:25:13
我在服务器上生成一个EXCEL文件,然后我想在客户端下载这个文件,该如何实现啊!!
补充一名,整个过程(生成excel文件,下载文件)都是在客户端的页面点击按钮实现的!
生成excel文件我已经写好了,就差下载了。大家帮帮忙吧!! 求了!!
...全文
97 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
jinlong5200 2008-05-30
  • 打赏
  • 举报
回复
以上是打开客户端访问服务器的Offce权限,必须打开才可以下载
jinlong5200 2008-05-30
  • 打赏
  • 举报
回复
如果出现以下错误:
Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005.
中文翻译为:检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80070005
解决办法:
点击运行键入dcomcnfg打开组件服务
依次展开"组件服务"->"计算机"->"我的电脑"->"DCOM配置"
找到"Microsoft Excel应用程序"
右键打开属性对话框
点击“标识” 选择交互式用户
点击"安全"选项卡,
把"启动和激活权限","配置权限",都选择为自定义,
然后依次点击它们的编辑,把ASPNET添加进去,并加入所有的权限...
OK,解决此问题!

jinlong5200 2008-05-30
  • 打赏
  • 举报
回复
protected void ButSave_Click(object sender, EventArgs e)
{
sendTableName = "Phone, Illness, See_Illness,Leechdom,Notes";
sendStrSQL = this.Label8.Text;
dataBase();
DataView dv = new DataView(ds.Tables[0]);
OutputExcel(dv,"塔里木石油医院慢性病用药报表");
}
public void OutputExcel(DataView dv, string str)
{
//
// TODO: 在此处添加构造函数逻辑
//
//dv为要输出到Excel的数据,str为标题名称
GC.Collect();
//Application excel;// = new Application();
int rowIndex = 2;
int colIndex = 0;
int SUM = 0;
int number = 0;
_Workbook xBk;
_Worksheet xSt;

Excel.ApplicationClass excel = new Excel.ApplicationClass();
xBk = excel.Workbooks.Add(true);

xSt = (_Worksheet)xBk.ActiveSheet;

//
//取得标题
//
foreach (DataColumn col in dv.Table.Columns)
{
colIndex++;
excel.Cells[2, colIndex] = col.ColumnName;
xSt.get_Range(excel.Cells[2, colIndex], excel.Cells[2, colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置标题格式为居中对齐
}

//
//取得表格中的数据
//
foreach (DataRowView row in dv)
{
rowIndex++;
colIndex = 0;
foreach (DataColumn col in dv.Table.Columns)
{
colIndex++;
//if (col.DataType == System.Type.GetType("System.DateTime"))
//{
// excel.Cells[rowIndex, colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
// xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐
//}
//else
if (col.DataType == System.Type.GetType("System.String"))
{

excel.Cells[rowIndex, colIndex] = "'" + row[col.ColumnName].ToString();

xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐
}
else
{
excel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
if (col.ColumnName.ToString() == "用药数量")
{
number = colIndex;
}
}
}
}
//
//加载一个合计行
//
int rowSum = rowIndex + 1;
int colSum = 1;
if (number != 0)
{
excel.Cells[rowSum, number] = this.lblnumber.Text;
}
excel.Cells[rowSum, 1] = "合计";
xSt.get_Range(excel.Cells[rowSum, 1], excel.Cells[rowSum, 1]).HorizontalAlignment = XlHAlign.xlHAlignCenter;

//if (row[col.ColumnName].ToString() = "用药数量")
//{

//}
//
//设置选中的部分的颜色
//
xSt.get_Range(excel.Cells[rowSum, colSum], excel.Cells[rowSum, colIndex]).Select();
xSt.get_Range(excel.Cells[rowSum, colSum], excel.Cells[rowSum, colIndex]).Interior.ColorIndex = 19;//设置为浅黄色,共计有56种
//
//取得整个报表的标题
//
excel.Cells[1, 1] = str;
//
//设置整个报表的标题格式
//
//xSt.get_Range(excel.Cells[1, 1], excel.Cells[1, 1]).Font.Bold = true;
xSt.get_Range(excel.Cells[1, 1], excel.Cells[1, 1]).Font.Size = 16;
//
//设置报表表格为最适应宽度
//
xSt.get_Range(excel.Cells[2, 1], excel.Cells[rowSum, colIndex]).Select();
xSt.get_Range(excel.Cells[2, 1], excel.Cells[rowSum, colIndex]).Columns.AutoFit();
//
//设置整个报表的标题为跨列居中
//
xSt.get_Range(excel.Cells[1, 1], excel.Cells[1, colIndex]).Select();
xSt.get_Range(excel.Cells[1, 1], excel.Cells[1, colIndex]).HorizontalAlignment = XlHAlign.xlHAlignCenterAcrossSelection;
//
//绘制边框
//
xSt.get_Range(excel.Cells[2, 1], excel.Cells[rowSum, colIndex]).Borders.LineStyle = 1;
xSt.get_Range(excel.Cells[2, 1], excel.Cells[rowSum, 1]).Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThick;//设置左边线加粗
xSt.get_Range(excel.Cells[2, 1], excel.Cells[2, colIndex]).Borders[XlBordersIndex.xlEdgeTop].Weight = XlBorderWeight.xlThick;//设置上边线加粗
xSt.get_Range(excel.Cells[2, colIndex], excel.Cells[rowSum, colIndex]).Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick;//设置右边线加粗
xSt.get_Range(excel.Cells[rowSum, 1], excel.Cells[rowSum, colIndex]).Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThick;//设置下边线加粗
//
//显示效果
//
excel.Visible = true;

xBk.SaveCopyAs(Server.MapPath(".") + "\\" + "2008.xls");

ds = null;
xBk.Close(false, null, null);

excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
xBk = null;
excel = null;
xSt = null;
GC.Collect();
string path = Server.MapPath("2008.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();

}
zu274568031 2008-05-30
  • 打赏
  • 举报
回复
string data = WriteDatagridToCsv(this.GridView1);
返回的是什么? 是csv名还是路径? 还是别的?
liyong11111 2008-05-30
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 a11377815 的回复:]
为什么要存在服务器呢?直接在客户端生成保存就OK了
[/Quote]
franksnow 2008-05-30
  • 打赏
  • 举报
回复
补充一点,你生成的XSL文件是什么名字,放在服务器哪个文件夹,你应该自己知道的吧。

客户端操作还是很麻烦的一件事情。
franksnow 2008-05-30
  • 打赏
  • 举报
回复
Response.Redirect("直接放出从根目录开始的文件路径");

记得设置IIS的MIME属性。有些扩展名可能没有,当URL定位到这些地址的时候会出现404或则其他异常错误。

其实你也可以用


Response.Charset = "GB2312";
Response.ContentType = "application/vnd.ms-excel";
Response.Write(data);


直接写到客户端,这样客户端应该会弹出下载框的吧。

zu274568031 2008-05-30
  • 打赏
  • 举报
回复
顺便问一下,像1楼那样做的话,能取到客户指定的目录吗?
zu274568031 2008-05-30
  • 打赏
  • 举报
回复
客户要指定文件目录啊,如果能弹出保存对话框,并能取得客户指定的目录
那就更好了。现在就是弹不出保存对话框所以才想到顾在服务器上再聊啊!!
a11377815 2008-05-30
  • 打赏
  • 举报
回复
为什么要存在服务器呢?直接在客户端生成保存就OK了
zu274568031 2008-05-30
  • 打赏
  • 举报
回复
.net爱好者兄,我怎么从服务器上取得我想要的那个文件啊!!!
franksnow 2008-05-30
  • 打赏
  • 举报
回复
我想到2种方法,不过不知道你的EXCEL保存为物理文件了没有。

方法一:

string data = WriteDatagridToCsv(this.GridView1);

string filenames = DateTime.Now.ToFileTime().ToString() + ".csv";

string temp = string.Format("attachment;filename={0}", filenames);

Response.ClearHeaders();
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AppendHeader("Content-disposition", temp);
Response.HeaderEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.Charset = "GB2312";
Response.ContentType = "application/vnd.ms-excel";
Response.Write(data);
Response.End();



方法二:(直接放出超链接提供下载)

string filenames = DateTime.Now.ToFileTime().ToString() + ".csv";

//string temp = string.Format("attachment;filename={0}", filenames);

string webpath = Server.MapPath("~/csvdata/") + filenames;

File.WriteAllText(webpath, data, Encoding.GetEncoding("GB2312"));
Response.Redirect("~/csvdata/" + filenames);

62,074

社区成员

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

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

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

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