哪位大神給指點一下,謝謝
uj5u.com熱心網友回復:
你這樣問還不如直接百度搜來的快。https://www.baidu.com/s?wd=asp.net%E5%AF%BC%E5%87%BAexcel&rsv_spt=1&rsv_iqid=0xf038f96c0003d5c1&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&tn=78040160_14_pg&ch=8&rsv_enter=1&rsv_dl=tb&rsv_sug3=18&rsv_sug1=3&rsv_sug7=100&rsv_t=d618zc7H%2BLbpwMSoOve9mJD2%2FH%2BfLTxRm5SNkl05T30%2BJsH9z3Iu%2BvY8T875auHLfB0qmBA&rsv_sug2=0&inputT=7206&rsv_sug4=7207或者搜NPOI
uj5u.com熱心網友回復:
你把問題分成兩部分,1、生成EXCEL。2、下載。uj5u.com熱心網友回復:
npoi spire.xls了解下,很簡單,網上一堆demo
uj5u.com熱心網友回復:
https://bbs.csdn.net/topics/390830774這是我之前發過的帖子.
下面是我下載的帖子
https://download.csdn.net/download/diaodiaop/7611721
uj5u.com熱心網友回復:
使用NPOI吧uj5u.com熱心網友回復:
Imports System.CollectionsImports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.IO
Imports System.Configuration
Imports System.Security.Permissions.FileDialogPermission
Imports System.Diagnostics
Public Class rep_jd
Inherits System.Web.UI.Page
#Region " Web 表單設計器生成的代碼 "
'該呼叫是 Web 表單設計器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents Tbyear As System.Web.UI.WebControls.TextBox
Protected WithEvents Ddlpart As System.Web.UI.WebControls.DropDownList
Protected WithEvents Ddl As System.Web.UI.WebControls.DropDownList
Protected WithEvents bt2 As System.Web.UI.WebControls.Button
Protected WithEvents lblstatus As System.Web.UI.WebControls.Label
'注意: 以下占位符宣告是 Web 表單設計器所必需的。
'不要洗掉或移動它。
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法呼叫是 Web 表單設計器所必需的
'不要使用代碼編輯器修改它。
InitializeComponent()
End Sub
#End Region
Dim cn As New SqlConnection(ConfigurationSettings.AppSettings("conn"))
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此處放置初始化頁的用戶代碼
Dim ss, uname, s4 As String
Dim unid, i As Integer
If Session("userid") Is Nothing Then
RegisterClientScriptBlock("提示:", "<script language=javascript>alert('超時,請重新登錄!')</script>")
Exit Sub
Else
If Not IsPostBack Then
Tbyear.Text = DateTime.Now.Year()
i = CInt(DateTime.Now.Month)
If i > 0 And i <= 7 Then
Ddlpart.SelectedValue = "上半年"
Else
Ddlpart.SelectedValue = "下半年"
End If
End If
End If
End Sub
Private Sub bt2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt2.Click
Dim ss, uname, s4 As String
Dim unid As Integer
Dim ds As New DataSet
Dim sy As String
Dim i, j As Integer
Dim fname As String = "fname"
Dim beforetime, aftertime As DateTime
beforetime = DateTime.Now 'add 2015
Dim xlApp As New Excel.Application
aftertime = DateTime.Now 'add 2015
Dim xlBooks As Excel.Workbooks, xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim spath As String
spath = Server.MapPath("./")
spath = spath & "uploads\pingwei4.xls"
xlApp.Visible = False
' xlBook = xlApp.Workbooks().Add
xlApp.DisplayAlerts = False
xlBooks = xlApp.Workbooks
xlBooks.Open(spath)
xlBook = xlBooks.Item(1)
'xlBook = xlApp.Application.Workbooks.Open("rep1.xlt", missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing)
xlSheet = xlBook.Worksheets("sheet1")
Dim Table As New DataTable
sy = DateTime.Now.Year
i = 0
Try
ss = "select unit,name,gid,pos,zhicheng,thisyear,partyear,huanjie,fenzu from pingwei where thisyear='"
ss = ss & sy & "' and partyear ='" & Ddlpart.SelectedValue.Trim & "' "
ss = ss & " and huanjie='" & Ddl.SelectedValue.Trim & "' order by fenzu,unit"
ds.Tables.Clear()
ds = Funlib.MyQueryUsesqlDataSet(ss, "temp")
If ds.Tables("temp").Rows.Count > 0 Then
xlApp.Cells(1, 5) = "單位評委報表"
xlApp.Cells(2, 5) = CStr(Funlib.getdate())
For i = 0 To ds.Tables("temp").Rows.Count - 1
For j = 0 To ds.Tables("temp").Columns.Count - 1
xlApp.Cells(i + 4, j + 1) = ds.Tables(0).Rows(i)(j)
s4 = ds.Tables(0).Rows(i)(3)
Next j
Next i
' xlSheet.get_Range(excel.cells(3,1),excel.cells(3,6).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//設定標題格式為居中對齊
i = ds.Tables("temp").Rows.Count
j = ds.Tables("temp").Columns.Count
With xlSheet
.Cells.Select()
.Cells.Columns.AutoFit()
.Range(.Cells(1, 3), .Cells(1, 3)).Font.Name = "黑體"
.Range(.Cells(1, 3), .Cells(1, 3)).Font.Size = 16
'設標題為黑體字
.Range(.Cells(1, 3), .Cells(1, 3)).Font.Bold = True
'標題字體加粗
.Range(.Cells(3, 1), .Cells(3, 10)).Font.Name = "黑體"
'設標題為黑體字
.Range(.Cells(3, 1), .Cells(3, 10)).Font.Bold = True
'標題字體加粗
.Range(.Cells(1, 3), .Cells(1, 3)).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter
.Range(.Cells(2, 3), .Cells(2, 3)).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter
.Range(.Cells(3, 1), .Cells(3, 10)).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter '//居中對齊
.Range(.Cells(3, 1), .Cells(i + 3, j)).Borders.LineStyle = 1
'設表格邊框樣式
.Range(.Cells(1, 3), .Cells(1, 10)).Style.wraptext = False
.Range(.Cells(3, 3), .Cells(i + 3, 10)).Style.wraptext = True
'自動換行
End With
Else
lblstatus.Text = "沒有符合條件的資料!"
End If
ds.Tables.Clear()
Catch ec As Exception
lblstatus.Text = "沒有符合條件的資料!"
Finally
End Try
Dim spath2 As String
spath2 = Server.MapPath("./") & "uploads\pingwei5.xls"
xlBook.SaveAs(spath2)
xlBook.Close()
xlApp.Quit()
xlApp = Nothing
Funlib.exec_gc()
Dim starttime As DateTime 'add 2015
Dim myProcesses() As Process
Dim myProcess As Process
myProcesses = Process.GetProcessesByName("excel")
For Each myProcess In myProcesses
starttime = myProcess.StartTime
If starttime > beforetime And starttime < aftertime Then
If myProcess.CloseMainWindow() = False Then
myProcess.Kill()
Exit For
End If
End If
Next
If File.Exists(spath2) = False Then
Exit Sub
Else
Page.Response.Clear()
Response.Expires = 0
Response.Buffer = True
Page.Response.AddHeader("Content-Type", "application/ms-excel")
Page.Response.AddHeader("Content-Disposition", "attachment;filename=" & Server.UrlEncode(spath2))
Page.Response.WriteFile(spath2)
Page.Response.End()
End If
End Sub
End Class
uj5u.com熱心網友回復:
以上代碼可正常運行uj5u.com熱心網友回復:
服務器上不裝Office,或者由于權限問題導致OLE組件權限不夠的話,完全無法運行。
讀寫Excel,建議使用NPOI,或者是我認為更好用的EPPlus(只適用于xlsx)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/32885.html
標籤:ASP.NET
上一篇:“Private Sub Button1_Click(sender As Object, e As System.EventArgs)”有多個帶有相同簽名的定義
下一篇:ZXING生成條形碼問題
