Web page data export method overview
Author: gnicky URL: http://blog.9cbs.net/loconfuse
Preface:
As the BS architecture is widely used, the corresponding data preservation techniques should also be improved, corresponding to the web page, that is, the HTML files we usually know, the files that make up the keyword and data mixing. The web page data export is simply that the data and format are separated, while saving data is another format.
text:
Analyze file header information such as HTTP Response, which is very useful for how to handle web pages, and simply introduce several common methods, as well as technological learning, non-theoretical learning.
The following code analysis how to generate Excel files for users to download by modifying the HTTP header file to achieve the functionality of data export. This non-actual ability to generate files in server is that the user does not occur in the same name of the server file file for the user of the request of the page! The general solution is based on user name, IP, random number, and the like.
Dim Resp AS HTTPRESPONSE
DIM colcount as integer = mydatagrid.columns.count - 1
Resp = Page.Response
Resp.contentencoding = system.text.Encoding.Getencoding ("GB2312") 'Resolves the key to Chinese garbled
'Resp.charset = "UTF-8"
'Resp.AddFileDependency (filename)
'Resp.conteType = "text / html"
'' Resp.Appendheader ("Content-Type", "Text / HTML; Charset = GB2312")
Resp.ContentType = "text / csv" 'You can let the user download as a CSV type file by modifying the file type, modify text / ** content
Resp.Appendheader ("Content-Disposition", "Attachment; FileName =" FileName ".csv") 'necessary, make download files
'Really look at the type of download of the download, you can make TXT, you can html, you can XLS, users may not know? And saved as XLS files, data is included in one column
Dim colheaders as string = ""
'Imports System.Text
DIM STRITEMS AS STRINGBUILDER = New StringBuilder
DIM Mycol as DataGridColumn
DIM I as integer
For i = 0 to colcount
Mycol = mydatagrid.columns (i)
If mycol.visible = true kil
Colheaders = colheaders & mycol.Headertext.toString & ","
END IF
NEXT
IF colheaders.length> 0 THEN
ColHeaders = colheaders.substring (0, colheaders.lastindexof (",")) End IF
Colheaders = colheaders & chr (13) & chr (10)
Resp.write (colheaders)
DIM Colrow As String
DIM ITEM AS DataGridItem
For Each Item in MyDataGrid.Items
Resp.write (formatexportrow (Colcount, Item, MyDataGrid)
'Write data to the corresponding cell
Next Item
Resp.end ()
There is also a description how to export a crystal report as a code saved by a PDF file, but this approach and the specific process of implementation are still in research. The basic idea is to open by PDF and then use the plug-in, or save the PDF directly.