如题!
实现的步骤?
------解决方案--------------------------------------------------------
可以将gridview导出excel,所以你要选择导出的列的话,可以先选择gridview绑定哪几个数据列,然后再将这个gridview导出,就可以实现了。
- C# code
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
再导出 项目利用的
- VB.NET code
'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は、枝番は不要(次の??????に出力) Dim strKoyuNo As String = Session("dsKoteiShisanList").Tables(0).Rows(intCnt).Item(i) intUBarPosi = InStrRev(strKoyuNo, "_") objSheet.Cells(intPasteRowNo, i + 2) = Mid(strKoyuNo, 1, intUBarPosi - 1) Else objSheet.Cells(intPasteRowNo, i + 2) = Session("dsKoteiShisanList").Tables(0).Rows(intCnt).Item(i) '施設CD~数量 End If Next For i = 34 To 45 objSheet.Cells(intPasteRowNo, i + 1) = Session("dsKoteiShisanList").Tables(0).Rows(intCnt).Item(i) '単位名称~期末簿価 Next '65000行までいったら、新しく???を作成 If intPasteRowNo - 3 = Common_Declare.cst_XlsMAXRowCnt Then '表外枠の下側??????下側に太罫線を引く With objSheet.Range("B3:AT3") .Borders(Common_Declare.xlEdgeBottom).LineStyle = Common_Declare.xlContinuous .Borders(Common_Declare.xlEdgeBottom).Weight = Common_Declare.xlMedium End With With objSheet.Range("B" & CStr(intPasteRowNo) & ":AT" & CStr(intPasteRowNo)) .Borders(Common_Declare.xlEdgeBottom).LineStyle = Common_Declare.xlContinuous .Borders(Common_Declare.xlEdgeBottom).Weight = Common_Declare.xlMedium End With '選択??をA1にしておく objSheet.Range("A1").Select() '次の???を生成 intSheetCnt = intSheetCnt + 1 '???数???????? Dim objSheet2 As Object 'Worksheet???????? objSheet2 = objBook.Worksheets(intSheetCnt) '???????????を取得 objSheet2.Copy(Before:=objSheet2) '?????????を???? objSheet = objBook.Worksheets(intSheetCnt) '???????????を再設定 objSheet.Name = cst_ExcelOpName & CStr(intSheetCnt) '???名を設定 intPasteRowNo = 3 '明細行位置初期値設定 End If intPasteRowNo = intPasteRowNo + 1 '次の明細の貼付け行Index Next '表外枠の下側??????下側に太罫線を引く With objSheet.Range("B3:AT3") .Borders(Common_Declare.xlEdgeBottom).LineStyle = Common_Declare.xlContinuous .Borders(Common_Declare.xlEdgeBottom).Weight = Common_Declare.xlMedium End With With objSheet.Range("B" & CStr(intPasteRowNo - 1) & ":AT" & CStr(intPasteRowNo - 1)) .Borders(Common_Declare.xlEdgeBottom).LineStyle = Common_Declare.xlContinuous .Borders(Common_Declare.xlEdgeBottom).Weight = Common_Declare.xlMedium End With 'Format???を削除 objBook.Worksheets(objBook.Worksheets.Count).Delete() '選択??をA1にしておく objSheet.Range("A1").Select() '1???目を??????にしておく objBook.Worksheets(1).Activate() '出力したExcel????を保存 'ExcelのVer.が2007より前かどうかを判断し、それぞれに合った??????を指定 If CType(objExcel.Version.ToString, Decimal) < 12 Then objBook.SaveAs(strSaveFileName) Else objBook.SaveAs(strSaveFileName, 56) End If Page.ClientScript.RegisterStartupScript(Me.GetType(), "PPP", "alert('" & cst_ExcelOpName & "の出力が完了しました。');", True) 'Range????????の解放 If (objDetailRange Is Nothing) = False Then System.Runtime.InteropServices.Marshal.ReleaseComObject(objDetailRange) End If 'excel????close If (objBook Is Nothing) = False Then objBook.close(False) System.Runtime.InteropServices.Marshal.ReleaseComObject(objBook) objBook = Nothing System.Runtime.InteropServices.Marshal.ReleaseComObject(objBooks) objBooks = Nothing objExcel.quit() System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcel) objExcel = Nothing End If GC.Collect() GC.WaitForPendingFinalizers() 'クライアントのExcel出力 Dim path As String = strSaveFileName Dim xFile As System.IO.FileInfo = New System.IO.FileInfo(path) Response.Clear() Response.Charset = "UTF-8" Response.ContentEncoding = System.Text.Encoding.UTF8 Response.AddHeader("Content-Disposition", "attachment; filename=" & Server.UrlEncode(xFile.Name)) Response.AddHeader("Content-Length", xFile.Length.ToString()) Response.ContentType = "application/ms-excel" Response.WriteFile(xFile.FullName)