打开使用EPPlus客户端Excel文件 [英] Opening a client-side Excel file using EPPlus

查看:339
本文介绍了打开使用EPPlus客户端Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写这也在客户端计算机上创建一个Excel文件,然后,他们保存和更新它,然后我需要再次导入,并将更改应用到我的数据库后,程序的中间。它的出口位工作正常,但是当我从Web应用程序中导入我得到了我的工作簿零表。如果我自己的计算机上运行它,即该网站是在同一台计算机上运行该文件位于然后正常工作



我的代码如下:

 
{
Productsfile =新的FileInfo(PathandFileName);
}
赶上(异常前)
{
lblStatus.Text =的String.Format(错误获取文件信息{0},ex.Message);
的回报;
}

lblStatus.Text + =文件打开成功地+ Productsfile.FullName + Environment.NewLine;

//打开和读取文件$ B $采用B试
{
(ExcelPackage数据文件=新ExcelPackage(Productsfile))
{

ExcelWorkbook WKB = datafile.Workbook;


如果(WKB!= NULL)
{

lblStatus.Text + =的String.Format(工作簿不为空,有{0}工作表,wkb.Worksheets.Count)+ Environment.NewLine;



我已经证实,该代码获得最后一个标签更新,还告诉我有零工作表该工作簿,但重要的是,工作簿本身不为空



当我们创建使用下面的代码的出口,该数据流从服务器传输到客户端 - 我我想知道,如果事情扭转它需要的:

  Response.ContentType =应用程序/ vnd.openxmlformats-officedocument.spreadsheetml.sheet  
Response.AddHeader(内容处置,附件;文件名= Products.xlsx);
Response.BinaryWrite(pck.GetAsByteArray());
到Response.End();


解决方案

右键,我相信我已经找到了答案:



1)获取从FileUpload控件
2文件详细信息)转换给内存流
3)使用内存流创建包



和代码如下:

 如果(FileUpload1。 HasFile)
{

HttpPostedFile文件= Request.Files [0];
myLabel.Text = file.FileName;
MemoryStream的纪念品=新的MemoryStream();
mem.SetLength((INT)file.ContentLength);

file.InputStream.Read(mem.GetBuffer(),0,(INT)file.ContentLength);

ExcelPackage包=新ExcelPackage(MEM);


ExcelWorksheet工作表= package.Workbook.Worksheets [1];
myLabel.Text + =+ worksheet.Name;

}


I am the middle of writing a program which has to create an excel file on a client machine and then, after they have saved and updated it then I need to import it again and apply the changes to my database. The export bit of it works fine, however when I import from the web application I get zero worksheets in my workbook. If I run it on my development machine, i.e. the website is running on the same machine as the file is located then it works fine.

My code is as follows:

try
    {
        Productsfile = new FileInfo(PathandFileName);
    }
    catch (Exception ex)
    {
        lblStatus.Text = string.Format("Error getting file info {0}", ex.Message);
        return;
    }

    lblStatus.Text += "File Opened succesfully " + Productsfile.FullName + Environment.NewLine;

    //Open and read file
    try
    {
        using (ExcelPackage datafile = new ExcelPackage(Productsfile))
        {

            ExcelWorkbook wkb = datafile.Workbook;


            if (wkb != null)
            {

                lblStatus.Text += string.Format("The workbook is not null and there are {0} worksheets", wkb.Worksheets.Count) + Environment.NewLine;

I have confirmed that the code gets to the last label update, which tells me there are zero worksheets in the workbook, but importantly that the workbook itself is not null.

When we created the export the following code was used, which transfers the data stream from server to client - I am wondering if something to reverse it is needed:

Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        Response.AddHeader("content-disposition", "attachment;  filename=Products.xlsx");
        Response.BinaryWrite(pck.GetAsByteArray());
        Response.End();

解决方案

Right, I believe I've found the answer:

1) Get the file details from a fileupload control 2) Convert this to a memory stream 3) Create the package using the memory stream

And the code is as follows:

if (FileUpload1.HasFile)
        {

            HttpPostedFile file = Request.Files[0];
            myLabel.Text = file.FileName;
            MemoryStream mem = new MemoryStream();
            mem.SetLength((int)file.ContentLength);

            file.InputStream.Read(mem.GetBuffer(), 0, (int)file.ContentLength);

            ExcelPackage package = new ExcelPackage(mem);


            ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
            myLabel.Text += " " + worksheet.Name;

        }

这篇关于打开使用EPPlus客户端Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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