Hello all,
I am trying to 1) export to excel, and 2) then format data using the below code and I can't seem to figure out how to integrate a mata to pre-specify width of columns.
My exportation code works perfectly and even the part where i use putexcel for formating. I know that putexcel cannot specify a cell width so i came across the folloing post which references this webpage but I have no idea which part is pertinent for me.
Is there anything in that webpage that might be useful for me to specifiy exactly the width of each exported column ? and where do I integrate that mata in my code (if i can)? I have never used mata so i am really not sure. If it's best to post this question in the mata forum i will modify it. Best wishes and thank you for any assistance.
I am trying to 1) export to excel, and 2) then format data using the below code and I can't seem to figure out how to integrate a mata to pre-specify width of columns.
My exportation code works perfectly and even the part where i use putexcel for formating. I know that putexcel cannot specify a cell width so i came across the folloing post which references this webpage but I have no idea which part is pertinent for me.
Is there anything in that webpage that might be useful for me to specifiy exactly the width of each exported column ? and where do I integrate that mata in my code (if i can)? I have never used mata so i am really not sure. If it's best to post this question in the mata forum i will modify it. Best wishes and thank you for any assistance.
* Variables to export
local vars Facteurs_de_risque IndicateurInformations Données_SLSJ Moyenne_Régionale
* Get climat values
levelsof climat, local(clims)
foreach c of local clims {
*------------------------------*
* File name = climat label
*------------------------------*
local clim_label : label (climat) `c'
local fname "`clim_label'"
local fname = subinstr("`fname'", " ", "_", .)
local fname = ustrnormalize("`fname'", "nfd")
local fname = ustrregexra("`fname'", "\p{M}", "")
local fname = subinstr("`fname'", "/", "-", .)
if "`fname'" == "" local fname "climat_`c'"
local outfile "export_`fname'.xlsx"
cap erase "`outfile'"
*------------------------------*
* Sous-composantes for this climat
*------------------------------*
levelsof souscompnum if climat == `c', local(subs)
foreach s of local subs {
*------------------------------*
* Sheet name = souscomp label
*------------------------------*
local sous_label : label (souscompnum) `s'
local sheet "`sous_label'"
local sheet = subinstr("`sheet'", " ", "_", .)
local sheet = ustrnormalize("`sheet'", "nfd")
local sheet = ustrregexra("`sheet'", "\p{M}", "")
local sheet = subinstr("`sheet'", "/", "-", .)
if "`sheet'" == "" local sheet "souscomp_`s'"
*------------------------------*
* EXPORT (CORRECT SYNTAX)
*------------------------------*
export excel `vars' ///
if climat == `c' & souscompnum == `s' ///
using "`outfile'", ///
sheet("`sheet'") sheetreplace ///
firstrow(varlabels)
*----------------------------------*
*----------------------------------*
* FORMATTING (wrap + left align)
*----------------------------------*
* Number of columns
local nvars : word count `vars'
local lastcol = char(64 + `nvars')
* Number of rows (+1 for header)
count if climat == `c' & souscompnum == `s'
local lastrow = r(N) + 1
* Ranges
local hdr "A1:`lastcol'1"
local body "A1:`lastcol'`lastrow'"
local last "A`lastrow':`lastcol'`lastrow'"
* Activate sheet
putexcel set "`outfile'", sheet("`sheet'") modify
* Header: bold, wrap, left aligned
putexcel `hdr', bold txtwrap left
* Body: wrap + left aligned
putexcel `body', txtwrap left
* Thick borders
putexcel `hdr', border(top, thick)
putexcel `hdr', border(bottom, thick)
putexcel `last', border(bottom, thick)
}
}
}
local vars Facteurs_de_risque IndicateurInformations Données_SLSJ Moyenne_Régionale
* Get climat values
levelsof climat, local(clims)
foreach c of local clims {
*------------------------------*
* File name = climat label
*------------------------------*
local clim_label : label (climat) `c'
local fname "`clim_label'"
local fname = subinstr("`fname'", " ", "_", .)
local fname = ustrnormalize("`fname'", "nfd")
local fname = ustrregexra("`fname'", "\p{M}", "")
local fname = subinstr("`fname'", "/", "-", .)
if "`fname'" == "" local fname "climat_`c'"
local outfile "export_`fname'.xlsx"
cap erase "`outfile'"
*------------------------------*
* Sous-composantes for this climat
*------------------------------*
levelsof souscompnum if climat == `c', local(subs)
foreach s of local subs {
*------------------------------*
* Sheet name = souscomp label
*------------------------------*
local sous_label : label (souscompnum) `s'
local sheet "`sous_label'"
local sheet = subinstr("`sheet'", " ", "_", .)
local sheet = ustrnormalize("`sheet'", "nfd")
local sheet = ustrregexra("`sheet'", "\p{M}", "")
local sheet = subinstr("`sheet'", "/", "-", .)
if "`sheet'" == "" local sheet "souscomp_`s'"
*------------------------------*
* EXPORT (CORRECT SYNTAX)
*------------------------------*
export excel `vars' ///
if climat == `c' & souscompnum == `s' ///
using "`outfile'", ///
sheet("`sheet'") sheetreplace ///
firstrow(varlabels)
*----------------------------------*
*----------------------------------*
* FORMATTING (wrap + left align)
*----------------------------------*
* Number of columns
local nvars : word count `vars'
local lastcol = char(64 + `nvars')
* Number of rows (+1 for header)
count if climat == `c' & souscompnum == `s'
local lastrow = r(N) + 1
* Ranges
local hdr "A1:`lastcol'1"
local body "A1:`lastcol'`lastrow'"
local last "A`lastrow':`lastcol'`lastrow'"
* Activate sheet
putexcel set "`outfile'", sheet("`sheet'") modify
* Header: bold, wrap, left aligned
putexcel `hdr', bold txtwrap left
* Body: wrap + left aligned
putexcel `body', txtwrap left
* Thick borders
putexcel `hdr', border(top, thick)
putexcel `hdr', border(bottom, thick)
putexcel `last', border(bottom, thick)
}
}
}

Comment