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

首頁 > 開發(fā) > 綜合 > 正文

將DataGrid中數(shù)據(jù)倒出Excel文件并下載

2024-07-21 02:29:54
字體:
供稿:網(wǎng)友
   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") '解決中文亂碼之關(guān)鍵
  '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菜鳥學(xué)堂:
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 石阡县| 鹤峰县| 都匀市| 二连浩特市| 德保县| 永胜县| 湖北省| 吉隆县| 房产| 顺义区| 余江县| 罗田县| 龙口市| 宝丰县| 茂名市| 通河县| 三穗县| 邢台县| 和平县| 达尔| 年辖:市辖区| 合江县| 拉孜县| 白山市| 高州市| 夏津县| 田东县| 临湘市| 枞阳县| 焉耆| 三原县| 丁青县| 喀什市| 博罗县| 西藏| 汉源县| 长治县| 文成县| 西平县| 乌拉特前旗| 容城县|