如何保存在ASP.NET创建的Excel文件,客户端PC [英] How to save created excel file to Client pc in ASP.NET

查看:147
本文介绍了如何保存在ASP.NET创建的Excel文件,客户端PC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的asp.net一个Excel报表和HTML报告时,我的短声按钮,我的应用程序创建并保存到客户端桌面,但由于Excel文件在服务器的桌面上创建它不能正常工作。如何解决这个问题?

感谢您的回复。

 字符串的currentdate;
日期时间的SaveNow = DateTime.Now;
的currentdate = saveNow.Date.ToShortDateString();
字符串reportContent = prepareHTM();字符串pathFile = Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory)+\\\\ As_Build_Report_+的currentdate +。html的;使用(StreamWriter的OUTFILE =新的StreamWriter(pathFile,真))
{
    outfile.WriteLine(reportContent);
}
System.Diagnostics.Process.Start(pathFile);


 对象缺少= System.Reflection.Missing.Value;
//启动Excel应用。
Excel.Application OXL =新Excel.Application();
oXL.Visible = TRUE; //显示应用。
Excel._Workbook OWB =(Excel._Workbook)(oXL.Workbooks.Add(失踪)); //创建一个新的工作簿。
Excel._Worksheet oSheet =(Excel._Worksheet)oWB.ActiveSheet; //创建一个表字符串的currentdate;
日期时间的SaveNow = DateTime.Now;
的currentdate = saveNow.Date.ToString();
INT保持= 5;
字符串的项目列表= [0]。项目;
oSheet.Cells [1,3] =MIKES配置管理__+项目+__竣工报告;
oSheet.Cells [3,1] =报告日期:;
oSheet.Cells [3,2] =的currentdate;oSheet.Cells [保管,1] =PART NO;
oSheet.Cells [保管,2] =REF.DES。
oSheet.Cells [保管,3] =说明;
oSheet.Cells [保管,4] =SERIAL NO;
oSheet.Cells [保持5] =C / S;
oSheet.Cells [保管,6] =D / C;
oSheet.Cells [保管,7] =备注;
保持++;
的foreach(在列表Classes.CMNewPart项)
{
    尝试
    {
        oSheet.Cells [保管,1] = item.PartNo;
        oSheet.Cells [保管,2] = item.RefDes;
        oSheet.Cells [保管,3] = item.Description1;
        oSheet.Cells [保管,4] = item.SerialNo;
        oSheet.Cells [保管,5] = item.Cs;
        oSheet.Cells [保管,6] = item.Dc;
        oSheet.Cells [保管,7] = item.Remark;
    }
    赶上(例外)
    {    }
    保持++;
}


解决方案

您需要使用Response对象将文件发送到客户端。要忽略客户端时,在打开Excel文件,如警告信息 -

要prevent这一点,你需要提及的响应内容类型和长度
使用示例code

  //读取字节数组Excel文件。这里PCK是Excelworkbook
字节] fileBytes = pck.GetAsByteArray();//清除响应
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.Cookies.Clear();
//添加标题和放大器;其他信息
Response.Cache.SetCacheability(HttpCacheability.Private);
Response.CacheControl =私人;
Response.Charset的= System.Text.UTF8Encoding.UTF8.WebName;
Response.ContentEncoding = System.Text.UTF8Encoding.UTF8;
Response.AppendHeader(内容长度,fileBytes.Length.ToString());
Response.AppendHeader(杂注,高速缓存);
Response.AppendHeader(过期,60);
Response.AppendHeader(内容处置,
附件;+
文件名= \\ExcelReport.xlsx \\;+
大小=+ fileBytes.Length.ToString()+,+
创建日期=+ DateTime.Now.ToString(R)+;+
修改的最新=+ DateTime.Now.ToString(R)+,+
读 - 日期=+ DateTime.Now.ToString(R));
Response.ContentType =应用程序/ vnd.openxmlformats-officedocument.s preadsheetml.sheet
//把它写回客户端
Response.BinaryWrite(fileBytes);
到Response.End();

I am creating a excel report and html report in asp.net when I clik the button and my application create it and save to the client desktop but, it is not working correctly because excel file is creating in server's desktop. How can I fix this matter ?

Thank you for your reply.

string CurrentDate;
DateTime saveNow = DateTime.Now;
CurrentDate = saveNow.Date.ToShortDateString();
string reportContent = prepareHTM();

string pathFile = Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory) + "\\As_Build_Report_ "+ CurrentDate + ".html";

using (StreamWriter outfile = new StreamWriter(pathFile, true))
{
    outfile.WriteLine(reportContent);
}
System.Diagnostics.Process.Start(pathFile);


object missing = System.Reflection.Missing.Value;
//Start Excel Application.
Excel.Application oXL = new Excel.Application();
oXL.Visible = true; //display Application .            
Excel._Workbook oWB = (Excel._Workbook)(oXL.Workbooks.Add(missing));//create a new workbook.
Excel._Worksheet oSheet = (Excel._Worksheet)oWB.ActiveSheet; //create a sheet                

string CurrentDate;
DateTime saveNow = DateTime.Now;
CurrentDate = saveNow.Date.ToString();
int keep = 5;
string project = list[0].Project;
oSheet.Cells[1, 3] = "MiKES Configuration Management __" + project + "__ As-Built Report";
oSheet.Cells[3, 1] = "Report Date :";
oSheet.Cells[3, 2] = CurrentDate;

oSheet.Cells[keep, 1] = "PART NO";
oSheet.Cells[keep, 2] = "REF.DES.";
oSheet.Cells[keep, 3] = "DESCRIPTION";
oSheet.Cells[keep, 4] = "SERIAL NO";
oSheet.Cells[keep, 5] = "C/S";
oSheet.Cells[keep, 6] = "D/C";
oSheet.Cells[keep, 7] = "REMARK";
keep++;
foreach(Classes.CMNewPart item in list)
{
    try
    {
        oSheet.Cells[keep, 1] = item.PartNo;
        oSheet.Cells[keep, 2] = item.RefDes;
        oSheet.Cells[keep, 3] = item.Description1;
        oSheet.Cells[keep, 4] = item.SerialNo;
        oSheet.Cells[keep, 5] = item.Cs;
        oSheet.Cells[keep, 6] = item.Dc;
        oSheet.Cells[keep, 7] = item.Remark;
    }
    catch (Exception)
    {

    }
    keep++;
}

解决方案

You need to send the file to client using Response object. To ignore the warning message like when client is opening the excel file -

To prevent this you need to mention the content type and length in the response use the sample code

//Read the Excel file in a byte array. here pck is the Excelworkbook              
Byte[] fileBytes = pck.GetAsByteArray();

//Clear the response               
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.Cookies.Clear();
//Add the header & other information      
Response.Cache.SetCacheability(HttpCacheability.Private);
Response.CacheControl = "private";
Response.Charset = System.Text.UTF8Encoding.UTF8.WebName;
Response.ContentEncoding = System.Text.UTF8Encoding.UTF8;
Response.AppendHeader("Content-Length", fileBytes.Length.ToString());
Response.AppendHeader("Pragma", "cache");
Response.AppendHeader("Expires", "60");
Response.AppendHeader("Content-Disposition",
"attachment; " +
"filename=\"ExcelReport.xlsx\"; " +
"size=" + fileBytes.Length.ToString() + "; " +
"creation-date=" + DateTime.Now.ToString("R") + "; " +
"modification-date=" + DateTime.Now.ToString("R") + "; " +
"read-date=" + DateTime.Now.ToString("R"));
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
//Write it back to the client    
Response.BinaryWrite(fileBytes);
Response.End();

这篇关于如何保存在ASP.NET创建的Excel文件,客户端PC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆