{"id":798,"date":"2008-05-28T03:05:20","date_gmt":"2008-05-28T03:05:20","guid":{"rendered":"http:\/\/6teen.ru\/?p=308"},"modified":"2008-05-28T03:05:20","modified_gmt":"2008-05-28T03:05:20","slug":"6945","status":"publish","type":"post","link":"http:\/\/pblog.ru\/lab\/?p=798","title":{"rendered":"\u041a\u0430\u043a \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 Excel \u0432 StringGrid"},"content":{"rendered":"<p>\u041a\u0430\u043a \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 Excel \u0432 StringGrid<br \/>\n<!--more--><\/p>\n<pre class=\"alt2\" style=\"margin:0px; padding:6px; border:1px inset; width:580px; height:320px; overflow:auto\"><div>\u041a\u0430\u043a \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 Excel \u0432 StringGrid\n\nuses\ncomobj;\n\nfunction xls_to_stringgrid(agrid: tstringgrid; axlsfile: string): boolean;\nconst\nxlcelltypelastcell = $0000000b;\nvar\nxlapp, sheet: olevariant;\nrangematrix: variant;\nx, y, k, r: integer;\nbegin\nresult := false;\n\/\/ create excel-ole object\nxlapp := createoleobject('excel.application');\ntry\n\/\/ hide excel\nxlapp.visible := false;\n\n\/\/ open the workbook\nxlapp.workbooks.open(axlsfile);\n\n\/\/ sheet := xlapp.workbooks[1].worksheets[1];\nsheet := xlapp.workbooks[extractfilename(axlsfile)].worksheets[1];\n\n\/\/ in order to know the dimension of the worksheet, i.e the number of rows\n\/\/ and the number of columns, we activate the last non-empty cell of it\n\nsheet.cells.specialcells(xlcelltypelastcell, emptyparam).activate;\n\/\/ get the value of the last row\nx := xlapp.activecell.row;\n\/\/ get the value of the last column\ny := xlapp.activecell.column;\n\n\/\/ set stringgrid's row &col dimensions.\n\nagrid.rowcount := x;\nagrid.colcount := y;\n\n\/\/ assign the variant associated with the worksheet to the delphi variant\n\nrangematrix := xlapp.range['a1', xlapp.cells.item[x, y]].value;\n\/\/ define the loop for filling in the tstringgrid\nk := 1;\nrepeat\nfor r := 1 to y do\nagrid.cells[(r - 1), (k - 1)] := rangematrix[k, r];\ninc(k, 1);\nagrid.rowcount := k + 1;\nuntil k > x;\n\/\/ unassign the delphi variant matrix\nrangematrix := unassigned;\n\nfinally\n\/\/ quit excel\nif not varisempty(xlapp) then\nbegin\n\/\/ xlapp.displayalerts := false;\nxlapp.quit;\nxlapp := unassigned;\nsheet := unassigned;\nresult := true;\nend;\nend;\nend;\n\nprocedure tform1.button1click(sender: tobject);\nbegin\nif xls_to_stringgrid(stringgrid1, 'c:\\table1.xls') then\nshowmessage('table has been exported!');\nend;\n<\/div><\/pre>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u041a\u0430\u043a \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 Excel \u0432 StringGrid<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[62,416],"tags":[1891,1927,211,769],"_links":{"self":[{"href":"http:\/\/pblog.ru\/lab\/index.php?rest_route=\/wp\/v2\/posts\/798"}],"collection":[{"href":"http:\/\/pblog.ru\/lab\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/pblog.ru\/lab\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/pblog.ru\/lab\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"http:\/\/pblog.ru\/lab\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=798"}],"version-history":[{"count":0,"href":"http:\/\/pblog.ru\/lab\/index.php?rest_route=\/wp\/v2\/posts\/798\/revisions"}],"wp:attachment":[{"href":"http:\/\/pblog.ru\/lab\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=798"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/pblog.ru\/lab\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=798"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/pblog.ru\/lab\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=798"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}