Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • putexcel formatting problems after formatting large range

    After running the code below, the text in Excel is not displaying as bold or properly centered (although this formatting is indicated on the ribbon). hcenter and bold can be swapped and either can be removed from the last line and the problem persists. In Excel, removing the text wrap causes the bold and/or centering to be displayed correctly.

    The problem does not occur if the range in the third line is reduced to A1:A16376 or any other range of 16,376 cells or less. The problem will occur even if the formatting of the large range is broken into several commands and even if a large set of smaller ranges are formatted across multiple sheets (in the same workbook/Excel file). Attempting to do the same formatting of the large range with the Mata xl() class results in the same problem (I assume putexcel is calling the xl() methods). Finally, an even larger formatting range (e.g., 100,000+) results in Excel needing to repair the file's font and formatting XML (this hasn't occurred in the main code I'm working on so it's less of an issue for me).

    I'm running Stata 14.2 (with 16 Mar 2017 updates), and the problem occurs opening the file in both the current and older (Office 2010) versions of Excel.

    Code:
    putexcel set "debug.xlsx", sheet("Test Sheet") replace
    putexcel A1 = "text text text text"
    putexcel A1:A16377, hcenter
    putexcel A1, txtwrap bold
    So far, there are two workarounds that I've found, but neither are particularly helpful for the reasons indicated below:

    1. Switch the third and last lines in the code above. In what I'm trying to ultimately accomplish, I'm writing to several sheets sequentially, and in this case, it's the large set of many smaller-range formatting commands that seems to be an issue. I'm not sure that there's a way to order my formatting commands in that context to avoid this issue (at least one hasn't occurred to me).

    2. Remove the text wrap in Excel, save the file and close, reopen and reapply the text wrap. I am trying to create multiple Excel files in an automated fashion. Having to perform these manual edits unfortunately defeats the purpose of the program I'm writing.

    Am I doing something wrong? If not, are there thoughts for other workarounds?

  • #2
    I have encountered a version of precisely the same problem (Stata 17.0, latest updates). For small putexcel examples, everything works as it should. Very recently I generated an XL file that included several (relatively) large collections (generated by [table (x y), stat() (stat) ...]), and I did apply some formatting to the x variables so they are larger and bolder, with the y underneath, deleted some titles, etc. through the [collect style] (header, cell x 3) commands.

    I exported the collection to a .docx document without any surprises [putdocx collect, name(c)]
    Without further addressing the collection in any way, I subsequently used [putexcel a3 = collect, name(c)], followed by [putexcel b3:e3, overwritefmt txtwrap right bold]

    My question (just in case anyone knows): In a "formatted" collection inserted into Excel, does each cell get its own formatting when exported, and this numerically challenges the XL-specific formatting space (as repeatedly warned against in the manuals when applying formats)?
    Secondary question: If so, is there a better way (short of not formatting the table)?

    Comment

    Working...
    X