Click here to Skip to main content
15,881,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
There is a table on the website and users enter data in the cells of the table. I edited the ready codes to pull the data entered by each user to different pages as follows. But the problem is: there are a lot of(2000) cells and I have to match them one by one in the code. I think this should be an easier way. I am waiting for your suggestions to me. (Sql will not be used) Thank you.


What I have tried:

C#
<pre><td class="auto-style6">%<asp:TextBox ID="txtC7" runat="server" Width="28px"></asp:TextBox>
        </td>
        <td class="auto-style7">%<asp:TextBox ID="txtD7" runat="server" Width="28px"></asp:TextBox></td>
    </tr>
    <tr>
        <td class="auto-style5">(%)</td>
        <td class="auto-style6">%<asp:TextBox ID="txtC8" runat="server" Width="28px"></asp:TextBox></td>
        <td class="auto-style7">%<asp:TextBox ID="txtD8" runat="server" Width="28px"></asp:TextBox></td>
    </tr>


C#
<pre>private static Microsoft.Office.Interop.Excel.Workbook mWorkBook;
    private static Microsoft.Office.Interop.Excel.Sheets mWorkSheets;
    private static Microsoft.Office.Interop.Excel.Application oXL;
    object mv= System.Reflection.Missing.Value;

    protected void Button1_Click(object sender, EventArgs e)
    {
        oXL = new Microsoft.Office.Interop.Excel.Application();
        oXL.Visible = true;
        oXL.DisplayAlerts = false;

        string path = "D:\\output.xls";
        if (!File.Exists(path))
        {
            mWorkBook = oXL.Workbooks.Add();
        }
        else
        {
            mWorkBook = oXL.Workbooks.Open(path, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true,
                false, 0, true, false, false);
        }
        mWorkSheets = mWorkBook.Worksheets;

        Microsoft.Office.Interop.Excel.Worksheet mWSheet = (Worksheet)mWorkBook.Worksheets.Add(mv, mv, mv, mv);
        mWSheet.Name = DateTime.UtcNow.ToString().Replace(":", "-"); //Replace(":", String.Empty);
        mWSheet.Range["B8"].Value = "(%)";
        mWSheet.Range["C7"].Value = "%" + txtC7.Text;
        mWSheet.Range["C8"].Value = "%" + txtC8.Text;
        mWSheet.Range["D7"].Value = "%" + txtD7.Text;
        mWSheet.Range["D8"].Value = "%" + txtD8.Text;

        mWorkBook.SaveAs(path, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal,
        mv, mv, mv, mv, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
        mv, mv, mv,
        mv, mv);

        mWorkBook.Close(mv, mv, mv);
        mWSheet = null;

        mWorkBook = null;

        oXL.Quit();
        GC.WaitForPendingFinalizers();
        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.Collect();
    }
Posted
Updated 3-Aug-20 21:16pm

You cannot use Office Interop in an ASP.NET application. It's not supported because Office does not support "re-entrancy". That means multiple session interactions to Excel can fail.

Sure, it'll owrk perfectly fine on your dev machine. You're one user testing the app. but when you put it into production with multiple users hitting the same code, you're going to run into a problem.

You have to use a different library to write an Excel sheet, like the OpenXML library, or ClosedXML. Just Google them and you'll find the them and plenty of examples on how to use them.
 
Share this answer
 
Well, you can try to export using JavaScript. Not the exact solve but would give you direction enough to get started: Import and Export Outlook Appointments (using JavaScript)[^]

You would need to find the excel equivalent and then try to export suing above idea.

PS: This solution though expects that client system has that assembly and Outlook installed. If this is not the case, you need to figure out some third party as Dave suggested.
 
Share this answer
 
v2
https://www.codeproject.com/Questions/65930/How-to-export-textbox-values-to-excel-sheet
 
Share this answer
 
Comments
Richard Deeming 4-Aug-20 12:27pm    
As solution 1 said, that's not going to work in an ASP.NET application.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900