Thursday 15 September 2011

vb.net - Read/ Write operations on .xlsx file -



vb.net - Read/ Write operations on .xlsx file -

there saved .xlsx file database ui. reading info database using below code , writing temp location file.

dim buffer() byte dim amount integer = 2000 dim tripcount integer = 0 dim reminder integer = 0 redim buffer(filesize) tripcount = filesize \ amount reminder = filesize mod amount if reminder > 0 tripcount = tripcount + 1 end if cnt integer = 0 tripcount - 1 dim offset integer = 1 offset = offset + cnt * amount dim readcount integer = amount if cnt = tripcount - 1 readcount = cint(filesize) - (cnt * amount) end if dim b() byte = getfiledata(clng(documentid), readcount, offset) 'getfiledata methdo lastly code segment array.copy(b, 0, buffer, offset - 1, b.length) next dim downloadfile new fileinfo(selectedpath + "\" + filename) using bw new binarywriter(downloadfile.openwrite) bw.write(buffer) bw.close() end using public function getfiledata(byval documentid long, byval amount integer, byval offset integer) byte() dim retval() byte = nil dim b1 byte() = nil seek dim sql string = "" redim retval(amount) sql = " select dbms_lob.substr(f.df_file," + amount.tostring + "," + offset.tostring + " ) data" sql += " table_name f" sql += " f.docid = " + documentid.tostring dim info datatable = fetchdata(sql) b1 = ctype(data.rows(0)("data"), byte()) array.copy(b1, 0, retval, 0, b1.length) grab ex exception end seek homecoming retval end function

after getting file temp location trying open it's throwing below error.

excel found unreadable content in 'filename.xlsx' want recover contents of workbook? if trust source of workbook, click yes

but same set of code working .xls format file.

the issue array size.

if file size 2000 bytes.

redim buffer(filesize)

after redim of buffer array size 2000 + 1 = 2001

then after getting database , putting buffer array , writing file below.

dim downloadfile new fileinfo(selectedpath + "\" + filename) using bw new binarywriter(downloadfile.openwrite) bw.write(buffer) bw.close() end using

the file size increased 1 byte. in case file size 2001 bytes. showing error.

now have replaced above below , it's working perfectly.

using bw new binarywriter(downloadfile.openwrite) dim newbuffer(((buffer.length) - 2)) byte array.copy(buffer, 0, newbuffer, 0, ((buffer.length) - 2)) bw.write(newbuffer) bw.close() end using

in above code have declared new array newbuffer , copying info old array buffer new array newbuffer 0 index (length of old array - 2) accomdate size of file.

vb.net excel vba excel-vba

No comments:

Post a Comment