开发者

How do I download an VSTO excel

I want to accomplish the following task.

  1. I have an web application through which I will be calling an VSTO apps, I have to insert some values in one of the sheets of the excel workbook

  2. I will be inserting the values in the excel sheet and will have to upload to DB using upload button.

What have I done so far.

  1. I created a web apps solution. "C:\Examples\WebVSTO\WebVSTO\WebVSTO"

  2. I created a VSTO solution. "C:\Examples\VSTO2007\VSTO2007\VSTO2007.xlsx"

  3. I insert the values in the excel sheet as below:

    System.Data开发者_高级运维.OleDb.OleDbConnection objConn = 
        new System.Data.OleDb.OleDbConnection(
        "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + 
        @"C:\Examples\VSTO2007\VSTO2007\VSTO2007.xlsx" +
        ";Extended Properties=Excel 8.0;");
    
    objConn.Open();
    
    System.Data.OleDb.OleDbCommand objCmd = new System.Data.OleDb.OleDbCommand();
    
    objCmd.Connection = objConn;
    
    objCmd.CommandText = "Insert into [Sheet1$]" + " values ('Test')";
    
    objCmd.ExecuteNonQuery();
    
    objConn.Close();
    
  4. I ran the web application, and observed that the values were inserted in the excel.

  5. I upload the values from the excel, by running the VSTO solution and the values got uploaded to the DB.

Issues

  1. The excel(only the VSTO excel and not the whole solution) will be placed in a different folder. When I hit the download button in the web apps, the excel will be downloaded in the clients place.

I face the above issue. How do I respond.

Kindly let me know.

Thanks in advance.


If I understand correctly, The question is quite a generic question: "How do I make a file not on the web application folder available for download?"

The simple way to do it is by writing the binary code into the Response Stream and setting some headers.

HttpResponse.Clear();
HttpResponse.BufferOutput = false;
HttpResponse.ContentType = "application/excel";
HttpResponse.AddHeader("content-disposition", "filename=VSTO2007.xlsx");

FileStream sourceFile = new FileStream(@"C:\Examples\VSTO2007\VSTO2007\VSTO2007.xlsx", FileMode.Open);
long FileSize;
FileSize = sourceFile.Length;
byte[] getContent = new byte[(int)FileSize];
sourceFile.Read(getContent, 0, (int)sourceFile.Length);
sourceFile.Close();

Response.BinaryWrite(getContent);

I didn't test it but it should work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜