日期:2014-05-17 浏览次数:20446 次
GridView gv_excel = new GridView(); gv_excel.AllowPaging = false; BindData(gv_excel);//这是我绑定gridview数据的方法,具体你不用关心实现,按你自己的需求写一个就可以了。 for (int i = 0; i < gv_excel.Rows.Count; i++) { gv_excel.Rows[i].Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@"); gv_excel.Rows[i].Cells[2].Attributes.Add("style", "vnd.ms-excel.numberformat:@"); gv_excel.Rows[i].Cells[4].Attributes.Add("style", "vnd.ms-excel.numberformat:0"); } Response.Clear(); Response.Buffer = true; Response.Charset = "GB2312"; Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls"); // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!! Response.ContentEncoding = System.Text.Encoding.UTF7; Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); gv_excel.RenderControl(oHtmlTextWriter); Response.Output.Write(oStringWriter.ToString()); Response.Flush(); Response.End();
------解决方案--------------------
出个参照,让用户区选择列,你根据列绑定数据,让后后导出
------解决方案--------------------
方案:
GridView Header部分加上一个 CheckBox,value属性记录数据库字段名字。导出的时候只导出这些字段内容
------解决方案--------------------
数据源包装的问题而已
遍历GridView到DS
再导出 项目利用的
'excel????????生成&(設定) objExcel = CreateObject("excel.application") objExcel.application.visible = False 'excelを非表示 Dim g_excelopdir As String = Common_AppSet.g_ExcelOpDir Dim g_excelformatdir As String = Common_AppSet.g_ExcelFormatDir strSaveFileName = g_excelopdir & Format(Now(), "yyyyMMdd HHmmss") & "_" & cst_ExcelFileName '出力????名生成 File.Copy(g_excelformatdir & cst_ExcelFileName, strSaveFileName, True) 'formatを???? objExcel.displayalerts = False '????を非表示 objExcel.enableevents = False '???を無効にする objBooks = objExcel.workbooks '????????????を取得 objBook = objBooks.open(strSaveFileName, False) 'excel????open objSheet = objBook.worksheets(1) '???????????を取得 objSheet.name = cst_ExcelOpName '???名を設定 '???数初期設定 intSheetCnt = 1 '作成日 objSheet.Cells(2, 6) = Format(Now(), "yyyy/MM/dd") '明細部の????元??範囲を指定 objDetailRange = objSheet.Range("B4:AT4") '明細行位置初期値設定 intPasteRowNo = 4 '明細数分枠線を描画して、????を書込する For intCnt = 0 To Session("dsKoteiShisanList").Tables(0).Rows.Count - 1 '明細部の枠線描画 objDetailRange.Copy() objSheet.Range("B" & CStr(intPasteRowNo)).Select() objSheet.Paste() '明細部の塗潰し If intPasteRowNo Mod 2 <> 0 Then objSheet.Range("B" & CStr(intPasteRowNo) & ":AT" & CStr(intPasteRowNo)).Interior.ColorIndex = Common_Declare.intLiteOrange End If '明細部????書込 For i = 0 To 32 If i = 2 Then '公有財産Noは、枝番は不要(次の??????に出力)