简体   繁体   中英

saving HTML table in “file.html” to excel

There was a recent Microsoft patch that broke html -> xls exports (which has since been fixed ).

Our current export process, basically just outputs what's within a repeater as an HTML string, and saves it as ".xls". This in turn causes a "file in different format..." warning when opening the file, and until the break patch, this was fine as you could confirm the warning, and still open the document.

There has been a patch that fixed this functionality that was introduced, but regardless I'm tasked with looking into a way to take the clients patch level out of the equation, and put it on our server's patch level. Actually the request is to recreate all of the exports, but I'm hoping there's an easier way, as the current is very generic and handles. Anyway...

test.html:

<html>
    <head>
        <title>Test Title</title>
    </head>
    <body>
        <table>
            <thead>
                <tr>
                    <td>Test Row 1</td>
                    <td>Test Row 2</td>
                </tr>
            </thead>
            <tbody>
                <tr><td>0</td><td>Test 0</td></tr>
                <tr><td>1</td><td>Test 1</td></tr>
                <tr><td>2</td><td>Test 2</td></tr>
                <tr><td>3</td><td>Test 3</td></tr>
                <tr><td>4</td><td>Test 4</td></tr>
                <tr><td>5</td><td>Test 5</td></tr>
                <tr><td>6</td><td>Test 6</td></tr>
                <tr><td>7</td><td>Test 7</td></tr>
                <tr><td>8</td><td>Test 8</td></tr>
                <tr><td>9</td><td>Test 9</td></tr>
                <tr><td>10</td><td>Test 10</td></tr>
                <tr><td>11</td><td>Test 11</td></tr>
                <tr><td>12</td><td>Test 12</td></tr>
                <tr><td>13</td><td>Test 13</td></tr>
                <tr><td>14</td><td>Test 14</td></tr>
                <tr><td>15</td><td>Test 15</td></tr>
                <tr><td>16</td><td>Test 16</td></tr>
                <tr><td>17</td><td>Test 17</td></tr>
                <tr><td>18</td><td>Test 18</td></tr>
                <tr><td>19</td><td>Test 19</td></tr>
                <tr><td>20</td><td>Test 20</td></tr>
                <tr><td>21</td><td>Test 21</td></tr>
                <tr><td>22</td><td>Test 22</td></tr>
                <tr><td>23</td><td>Test 23</td></tr>
                <tr><td>24</td><td>Test 24</td></tr>
            </tbody>
        </table>
    </body>
</html>

Which looks like this opened in excel:

在此处输入图片说明

I'm (attempting) to use excel interop, to open the file, and resave it as a xls/xlsx extension (hoping it will be similar to a manual "save as" from excel), at which point the warning about different document format will go away (hopefully).

I would have thought it'd be as simple as this:

string htmlFilePathAndName = @"C:/test.html";
string newXlsxFilePathAndName = @"C:/test.xlsx";

Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook xls;
xls = excel.Workbooks.Open(htmlFilePathAndName);
xls.SaveAs(newXlsxFilePathAndName, XlFileFormat.xlOpenXMLWorkbook); // exception

but I get an exception:

Unhandled Exception: System.Runtime.InteropServices.COMException: Microsoft Exce l cannot access the file 'C://7A133BE0'. There are several possible reasons:

The file name or path does not exist.

The file is being used by another program.

The workbook you are trying to save has the same name as a currently open workb ook.

at Microsoft.Office.Interop.Excel._Workbook.SaveAs(Object Filename, Object Fi leFormat, Object Password, Object WriteResPassword, Object ReadOnlyRecommended, Object CreateBackup, XlSaveAsAccessMode AccessMode, Object ConflictResolution, O bject AddToMru, Object TextCodepage, Object TextVisualLayout, Object Local)

I'm not sure what the "7A133BE0" comes from, but I'd guess it's a temporary working file, as the value changes with each attempted run.

Is there another way to do what I'm trying to do, in a programmatic way? I'd really like to avoid trying to recreate the website tables in a viewmodel and attempting to find a generic way to write out said viewmodel in closedxml

The issue is with the file path @"C:/test.html"; @"C:/test.xlsx";

should be

@"C:\\test.html"; @"C:\\test.xlsx";

Please rate the answere if this helps you.

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