国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

實現將DataGrid中數據倒出Excel文件并下載

2024-07-21 02:23:57
字體:
來源:轉載
供稿:網友
imports system
imports system.text
namespace toexcel

    '功能:將asp.net中datagrid生成excel文件下載。
    'mountains改進:1、支持中文 2、隱藏列不顯示
    '日期:2002.10.30
    public class datagridtocsv

        public function generatefile(byref page as system.web.ui.page, byval mydatagrid as system.web.ui.webcontrols.datagrid, byval filename as string) as string

            dim resp as httpresponse
            dim colcount as integer = mydatagrid.columns.count - 1

            resp = page.response

            resp.contentencoding = system.text.encoding.getencoding("gb2312") '解決中文亂碼之關鍵
            'resp.charset = "utf-8"
            'resp.addfiledependency(filename)
            'resp.contenttype = "text/html"
            ''resp.appendheader("content-type", "text/html; charset=gb2312")

            resp.appendheader("content-disposition", "attachment;filename=" + filename)  '必要,做成下載文件
   

            dim colheaders as string = ""
            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 then
                    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))
            next item

            resp.end()

        end function

        private function formatexportrow(byval colcount as integer, byval item as datagriditem, byval mydatagrid as system.web.ui.webcontrols.datagrid) as string
            dim stritem as string
            dim i as integer

            for i = 0 to colcount
                if mydatagrid.columns(i).visible = true then
                    if item.cells(i).text is system.dbnull.value then
                        item.cells(i).text = ""
                    end if
                    if i = colcount then
                        stritem += item.cells(i).text.tostring & chr(13) & chr(10)
                    else
                        stritem += item.cells(i).text.tostring & ","
                    end if
                end if
            next
            stritem = replace(stritem, " ", " ")
            return stritem
        end function


    end class

end namespace
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 射洪县| 邵东县| 徐水县| 宁远县| 静乐县| 百色市| 大姚县| 抚远县| 吉林省| 古浪县| 凤凰县| 克东县| 营山县| 阿克苏市| 雷州市| 油尖旺区| 密山市| 辉南县| 桑植县| 新密市| 比如县| 澎湖县| 沾益县| 拉孜县| 黄梅县| 海丰县| 泰州市| 永济市| 金坛市| 嵩明县| 汶上县| 南投市| 宜宾市| 西城区| 东乡县| 从江县| 镇康县| 托里县| 嘉鱼县| 河津市| 林周县|