├── .gitignore ├── readme.md ├── source └── aspExl.class.asp └── test └── default.asp /.gitignore: -------------------------------------------------------------------------------- 1 | #OS junk files 2 | [Tt]humbs.db 3 | *.DS_Store 4 | 5 | #Visual Studio files 6 | *.[Oo]bj 7 | *.exe 8 | *.pdb 9 | *.user 10 | *.aps 11 | *.pch 12 | *.vspscc 13 | *.vssscc 14 | *_i.c 15 | *_p.c 16 | *.ncb 17 | *.suo 18 | *.tlb 19 | *.tlh 20 | *.bak 21 | *.[Cc]ache 22 | *.ilk 23 | *.log 24 | *.lib 25 | *.sbr 26 | *.sdf 27 | *.opensdf 28 | *.unsuccessfulbuild 29 | ipch/ 30 | obj/ 31 | [Bb]in 32 | [Dd]ebug*/ 33 | [Rr]elease*/ 34 | Ankh.NoLoad 35 | 36 | #MonoDevelop 37 | *.pidb 38 | *.userprefs 39 | 40 | #InstallShield 41 | [Ss]ingle[Ii]mage/ 42 | [Dd][Vv][Dd]-5/ 43 | [Ii]nterm/ 44 | 45 | #Tooling 46 | _ReSharper*/ 47 | *.resharper 48 | [Tt]est[Rr]esult* 49 | 50 | #Project files 51 | [Bb]uild/ 52 | 53 | #Subversion files 54 | .svn 55 | 56 | # Office Temp Files 57 | ~$* 58 | 59 | 60 | #NuGet 61 | packages/ 62 | 63 | #ncrunch 64 | *ncrunch* 65 | *crunch*.local.xml 66 | 67 | # visual studio database projects 68 | *.dbmdl -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | #aspXLS 1.0 2 | 3 | A classic ASP Excel writer that currently supports building and writing CSV, TSV (tab separated values) 4 | and HTML outputing, including a pretty print for HTML. 5 | 6 | There are plans of writing to XLS and XLSX formats in a near future and reading capabilities for each 7 | of the supported file formats: CSV, TSV, XLS and XLSX. 8 | 9 | ##License 10 | 11 | **The MIT License (MIT) - http://opensource.org/licenses/MIT** 12 | Copyright (c) 2012 RCDMK 13 | 14 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 15 | associated documentation files (the "Software"), to deal in the Software without restriction, 16 | including without limitation the rights to use, copy, modify, merge, publish, distribute, 17 | sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 18 | furnished to do so, subject to the following conditions: 19 | 20 | The above copyright notice and this permission notice shall be included in all copies or substantial 21 | portions of the Software. 22 | 23 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 24 | NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 25 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 26 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT 27 | OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 28 | 29 | 30 | ##Easy to use 31 | 32 | Instantiate the class: 33 | 34 | set xls = new aspExl 35 | 36 | Add the header/titles for your structure: 37 | 38 | ' Add a header: setHeader(x, value) 39 | xls.setHeader 0, "id" 40 | xls.setHeader 1, "description" 41 | xls.setHeader 2, "createdAt" 42 | 43 | Add some data: 44 | 45 | ' Add the first data row: setValue(x, y, value) 46 | xls.setValue 0, 0, 1 47 | xls.setValue 1, 0, "obj 1" 48 | xls.setValue 2, 0, date() 49 | 50 | ' Add a range of values at once: setRange(x, y, valuesArray) 51 | xls.setRange 0, 2, Array(2, "obj 2", #11/25/2012#) 52 | 53 | 54 | **Easy for outputing:** 55 | 56 | Output the data in string formatted values: 57 | 58 | outputCSV = xls.toCSV() 59 | outputTSV = xls.toTabSeparated() 60 | 61 | outputHTML = xls.toHtmlTable() 62 | 63 | xls.prettyPrintHTML = true 64 | outputPrettyHTML = xls.toHtmlTable() 65 | 66 | 67 | Or write it directly to a file: 68 | 69 | ' Write the output to a file: writeToFile(filePath, format) 70 | xls.writeToFile("c:\mydata.csv", ASPXLS_CSV) 71 | 72 | The format flags supported are: 73 | 74 | ASPXLS_CSV = 1 ' CSV format 75 | ASPXLS_TSV = 2 ' Tab separeted format 76 | ASPXLS_HTML = 3 ' HTML table format -------------------------------------------------------------------------------- /source/aspExl.class.asp: -------------------------------------------------------------------------------- 1 | <% 2 | ' Classic ASP CSV creator 1.0 3 | ' By RCDMK 4 | ' 5 | ' The MIT License (MIT) - http://opensource.org/licenses/MIT 6 | ' Copyright (c) 2012 RCDMK 7 | ' 8 | ' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 9 | ' associated documentation files (the "Software"), to deal in the Software without restriction, 10 | ' including without limitation the rights to use, copy, modify, merge, publish, distribute, 11 | ' sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 12 | ' furnished to do so, subject to the following conditions: 13 | ' 14 | ' The above copyright notice and this permission notice shall be included in all copies or substantial 15 | ' portions of the Software. 16 | ' 17 | ' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 18 | ' NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | ' NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 20 | ' DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT 21 | ' OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | 24 | ' Format constants 25 | const ASPXLS_CSV = 1 ' CSV format 26 | const ASPXLS_TSV = 2 ' Tab separeted format 27 | const ASPXLS_HTML = 3 ' HTML table format 28 | 29 | 30 | ' Main class 31 | class aspExl 32 | dim lines(), curBoundX, curBoundY 33 | dim headers() 34 | dim m_prettyPrintHTML 35 | 36 | 37 | ' A flag for outputing more readable HTML 38 | public property get prettyPrintHTML() 39 | prettyPrintHTML = m_prettyPrintHTML 40 | end property 41 | 42 | public property let prettyPrintHTML(byval value) 43 | m_prettyPrintHTML = value 44 | end property 45 | 46 | 47 | ' Initialization and destruction 48 | sub class_initialize() 49 | curBoundX = -1 50 | curBoundY = -1 51 | m_prettyPrintHTML = false 52 | end sub 53 | 54 | sub class_terminate() 55 | ' Destroy all elements of the arrays 56 | redim lines(-1) 57 | redim headers(-1) 58 | end sub 59 | 60 | 61 | ' Resizes the columns and header arrays to fit a new size 62 | private sub resizeCols(byval newSize) 63 | dim i, cols 64 | 65 | for i = 0 to curBoundY 66 | cols = lines(i) 67 | redim preserve cols(newSize) 68 | lines(i) = cols 69 | next 70 | 71 | redim preserve headers(newSize) 72 | 73 | curBoundX = newSize 74 | end sub 75 | 76 | 77 | ' Resizes the lines array to fit a new size 78 | private sub resizeRows(byval newSize) 79 | dim i 80 | redim preserve lines(newSize) 81 | 82 | for i = curBoundY + 1 to newSize 83 | if i >= 0 then lines(i) = Array() 84 | next 85 | 86 | curBoundY = newSize 87 | 88 | resizeCols curBoundX 89 | end sub 90 | 91 | 92 | ' Contatenates and return the values in a string using a separator string 93 | private function toString(byval separator) 94 | dim output, headersString, i 95 | output = "" 96 | headersString = join(headers, separator) 97 | 98 | if replace(headersString, separator, "") <> "" then output = headersString & vbCrLf 99 | 100 | for i = 0 to curBoundY 101 | output = output & join(lines(i), separator) & vbCrLf 102 | next 103 | 104 | toString = output 105 | end function 106 | 107 | 108 | ' Sets a header value 109 | public sub setHeader(byval x, byval value) 110 | if x > curBoundX then resizeCols(x) 111 | 112 | headers(x) = value 113 | end sub 114 | 115 | 116 | ' Sets the value of a cell 117 | public sub setValue(byval x, byval y, byval value) 118 | dim cols 119 | 120 | if y > curBoundY then resizeRows y 121 | if x > curBoundX then resizeCols x 122 | 123 | cols = lines(y) 124 | cols(x) = value 125 | 126 | lines(y) = cols 127 | end sub 128 | 129 | 130 | ' Sets the values of a range of cells starting at the specified coordinates 131 | public sub setRange(byval x, byval y, byval arr) 132 | if y > curBoundY then resizeRows y 133 | 134 | dim arrBound 135 | arrBound = ubound(arr) 136 | 137 | if arrBound + x > curBoundX then resizeCols(arrBound + x) 138 | 139 | dim i, cols 140 | cols = lines(y) 141 | 142 | for i = 0 to arrBound 143 | cols(x + i) = arr(i) 144 | next 145 | 146 | lines(y) = cols 147 | end sub 148 | 149 | 150 | ' Returns a string formatted output of the data 151 | public function outputTo(byval format) 152 | dim output, headersString, i 153 | 154 | select case format 155 | case ASPXLS_HTML: 156 | output = "" 157 | headersString = join(headers, "" 160 | 161 | output = output & "" 162 | 163 | for i = 0 to curBoundY 164 | output = output & "" 165 | next 166 | 167 | output = output & "
") 158 | 159 | if replace(headersString, "", "") <> "" then output = output & "
" & headersString & "
" & join(lines(i), "") & "
" 168 | 169 | 170 | ' Prettify HTML for easy reading 171 | if m_prettyPrintHTML then 172 | dim lineSeparator, indentChar 173 | dim regex, breakAndIndent, doubleIndent 174 | 175 | lineSeparator = vbCrLf 176 | indentChar = vbTab 177 | 178 | breakAndIndent = lineSeparator & indentChar 179 | doubleIndent = indentChar & indentChar 180 | 181 | set regex = new regexp 182 | regex.global = true 183 | 184 | regex.pattern = "()" 185 | output = regex.replace(output, breakAndIndent & "$1") 186 | 187 | regex.pattern = "()" 188 | output = regex.replace(output, breakAndIndent & indentChar & "$1") 189 | 190 | regex.pattern = "()" 191 | output = regex.replace(output, lineSeparator & "$1") 192 | 193 | regex.pattern = ">(<(?:th|td)>)" 194 | output = regex.replace(output, ">" & breakAndIndent & doubleIndent & "$1") 195 | 196 | set regex = nothing 197 | end if 198 | 199 | case ASPXLS_CSV: 200 | output = toString(";") 201 | 202 | case ASPXLS_TSV: 203 | output = toString(vbTab) 204 | 205 | case default: 206 | output = "" 207 | end select 208 | 209 | outputTo = output 210 | end function 211 | 212 | 213 | ' Returns a semi-colon separated string for each row 214 | public function toCSV() 215 | toCSV = outputTo(ASPXLS_CSV) 216 | end function 217 | 218 | 219 | ' Returns a TAB separated string for each row 220 | public function toTabSeparated() 221 | toTabSeparated = outputTo(ASPXLS_TSV) 222 | end function 223 | 224 | 225 | ' Returns a HTML table formatted string 226 | public function toHtmlTable() 227 | toHtmlTable = outputTo(ASPXLS_HTML) 228 | end function 229 | 230 | 231 | ' Writes a string fomatted output to a file 232 | public sub writeToFile(byval filePath, byval format) 233 | dim fso, file 234 | dim i 235 | set fso = createObject("scripting.filesyStemObject") 236 | 237 | set file = fso.createTextFile(filePath, true) 238 | 239 | file.writeLine outputTo(format) 240 | 241 | file.close 242 | set file = nothing 243 | 244 | set fso = nothing 245 | end sub 246 | end class 247 | %> 248 | -------------------------------------------------------------------------------- /test/default.asp: -------------------------------------------------------------------------------- 1 | <% 2 | option explicit 3 | 4 | %> 5 | 6 | 7 | 8 | aspExl Tests 9 | 10 | 15 | 16 | 17 | 24 |
25 |
26 |
27 |

Examples

28 | <% 29 | dim startTime, xls, i 30 | startTime = timer 31 | 32 | set xls = new aspExl 33 | 34 | ' Add a header 35 | xls.setHeader 0, "id" 36 | xls.setHeader 1, "description" 37 | xls.setHeader 2, "createdAt" 38 | 39 | ' Add the first data row 40 | xls.setValue 0, 0, 1 41 | xls.setValue 1, 0, "obj 1" 42 | xls.setValue 2, 0, date() 43 | 44 | ' Add a bigger imcomplete data row 45 | xls.setValue 3, 1, "Comment" 46 | 47 | ' Add a range of values at once 48 | xls.setRange 0, 2, Array(2, "obj 2", #11/25/2012#) 49 | 50 | 51 | ' Add lots of data 52 | for i = 3 to 1000 53 | xls.setValue 0, i, i 54 | xls.setValue 1, i, "obj " & i 55 | xls.setValue 2, i, now() 56 | next 57 | 58 | 59 | dim outputCSV, outputTSV, outputHTML, outputPrettyHTML 60 | 61 | outputCSV = xls.toCSV() 62 | outputTSV = xls.toTabSeparated() 63 | 64 | outputHTML = xls.toHtmlTable() 65 | 66 | xls.prettyPrintHTML = true 67 | outputPrettyHTML = xls.toHtmlTable() 68 | 69 | %> 70 |

toCSV()

71 |
<%= outputCSV %>
72 | 73 |

toTabSeparated()

74 |
<%= outputTSV %>
75 | 76 |

toHtmlTalbe() - prettyPrintHTML = false (default)

77 |
<%= Server.HtmlEncode(outputHTML) %>
78 | 79 |

toHtmlTalbe() - prettyPrintHTML = true

80 |
<%= Server.HtmlEncode(outputPrettyHTML) %>
81 | 82 |

HTML

83 | <%= replace(replace(replace(outputHTML, "", ""), " 84 | 85 | <% 86 | set xls = nothing 87 | %> 88 |
89 |
90 |
91 | 101 | 102 | --------------------------------------------------------------------------------