简体   繁体   中英

VB.NET - save excel file opened by a web application

Using VB.NET, I'm trying to save an excel file opened by a web application by clicking on the export button (file remains open with a temp name as 'Book1'). I'm trying to create an exe file using vb.net which will save the existing excel file into a given format, say xlsx or xls or html. This is a mini automation project that I want to implement with a standard product "VB.NET"

I'm well versed in vba, where in I use the workbook index to loop on all the existing files without knowing the name of the file, in vb.net (which i'm new at) I couldn't. It's a mini-mini project, that I'm trying to implement.

Issue: Excel Interop is unable to recognize the opened file.

While getting the count using "workbook.count" it reports zero, but if the file is created by interop using "workbook.add" it reports the count as 1.

Imports Microsoft.Office.Interop.Excel
Imports Microsoft.Office.Interop

Dim xlApp As New Application
fileCount1 = xlApp.Workbooks.Count

filecount1 = 0 (while watching it after the last line of above code)

Can someone direct me towards a solution.

thanks!!

Updates: The main idea is to use .net or vbscript or any other technology to save the file as xls or html format. This is part of the automation testing that we are trying to implement. I'm targetting vb.net as it would have better integration with MS Excel. This piece of external exe (.net) or function (vbscript) can be triggered from the automation tool.

Updates on the question, kinda settled on using vbscript to save any excel stream opened from the application.

With vb.net functions, I had the issue that excel.interop not recognizing the already open file (which was opened by some external program).

Under some instances, where excel file was not recognized by vbscript, I tried to set the focus on a different object collection like browser and setting it back on excel, I was able to recognize it.

Thanks for all the help.

You need to get a reference to the Excel application opened by your web application and still running. When you have this reference you could work with the Excel.Interop and save/close your workbook with the desired name

public bool SaveOpenExcel(string fileName)
{
    int iSection = 0, iTries = 0;
    Microsoft.Office.Interop.Excel.Application oExcel;

    tryAgain: 
    try 
    {      
        iSection = 1; // Attempting GetObject.
        oExcel = (Microsoft.Office.Interop.Excel.Application)
                System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
        iSection = 0; // Resume normal error handling.

        oExcel.ActiveWorkbook.SaveAs(@"d:\temp\running.xlsx", 
                Microsoft.Office.Interop.Excel.XlFileFormat.xlXMLSpreadsheet,
                System.Type.Missing, System.Type.Missing, System.Type.Missing, 
                System.Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, 
                System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, 
                System.Type.Missing);

        oExcel.Quit();
        oExcel = null;
        return true;
    } 
    catch (Exception err) 
    {
        if (iSection == 1) 
        { 
            //GetObject may have failed because the
            //Shell function is asynchronous; enough time has not elapsed
            //for GetObject to find the running Office application. Wait
            //1/2 seconds and retry the GetObject. If you try 20 times
            //and GetObject still fails, assume some other reason
            //for GetObject failing and exit the procedure.
            iTries++;
            if (iTries < 20) 
            {
                System.Threading.Thread.Sleep(500); // Wait 1/2 seconds.
                goto tryAgain; //resume code at the GetObject line
            } 
            else
            {
                Console.WriteLine("GetObject still failing.  Process ended.");
                return false;
            }
        } 
        else 
        {   
            //iSection = 0 so use normal error handling:
            Console.WriteLine(err.Message);
            return false;
        }
    }
}

This code sample has been adapted from the Microsoft Support Article regarding a similar issue with WinWord. You could read the article to better understand the problems with this kind of approach.
I really suggest to change your Web Application to complete its task saving the file.

Dim fileTest As String = "C:\Temp\ExcelTest\test.xlsx"

    If File.Exists(fileTest) Then

        File.Delete(fileTest)

    End If

    Dim oExcel As Object

    oExcel = CreateObject("Excel.Application")

    Dim oBook As Excel.Workbook

    Dim oSheet As Excel.Worksheet

    oBook = oExcel.Workbooks.Add

    oSheet = oExcel.Worksheets(1)

    oSheet.Name = "Test Name"

    oSheet.Range("A1").Value = "SOME VALUE"

    oBook.SaveAs(fileTest)

    oBook.Close()

    oBook = Nothing

    oExcel.Quit()

    oExcel = Nothing

http://howtodomssqlcsharpexcelaccess.blogspot.ca/2012/08/vbnet-how-to-creae-excel-file-simple.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM