摘要:NPOI上傳並[讀取]檔案,之後[添加(加入)]新資料
把兩個範例,先學通了!
就能千變萬化
兩個範例「組合」起來,又是一個新功能。
我們會先用以前做過的範例
在 Server 端存取 Excel 檔案的利器:NPOI Library v1.21(改寫成VB範例)
第一,上傳並且讀取 Excel檔案
如上文的範例四
第二,產生新資料、新的工作表,加入 Excel檔裡面
如上文的範例一
====================================================================================
範例四: 把手邊的 Excel檔案上傳(透過 FileUpload控制項)讀取,轉換成 DataTable
並轉換成 GridView呈現在 Web畫面上
這個範例的難度略高了一些,
我加上自己的註解(但,不保證正確),希望對您有幫助
本範例的 Excel上傳,僅限於 Excel 2003格式。
以下範例可用於 NPOI 1.2.1版。 1.2.4與 1.2.5版都可以。
不同版本,上方的 NameSpace不同,請自己增減。

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

本範例的 Excel上傳,僅限於 Excel 2003格式。
====================================================================================
上面提到的 NPOI 1.2.4版範例,
可由此下載 -- http://www.dotblogs.com.tw/mis2000lab/archive/2011/12/01/npoi_v124_sample_download_20111201.aspx
(1) . 請把上述 68~70列的程式碼,註解掉。
(2). 把下面程式,加入 76~78列的空白裡面。完成了
11 Dim ms As MemoryStream = New MemoryStream() '==需要 System.IO命名空間
12
13 '== 新增試算表。
14 '== 生成一個空白的 Excel 檔案,並且添加三個指定名稱的試算表 Sheet
15 workbook.CreateSheet("試算表 Sheet A")
16 workbook.CreateSheet("試算表 Sheet B")
17 workbook.CreateSheet("試算表 Sheet C")
18
19 workbook.Write(ms)
20 '== Excel檔名,請寫在最後面 filename的地方
21 Response.AddHeader("Content-Disposition", String.Format("attachment; filename=你的檔名.xls"))
22 Response.BinaryWrite(ms.ToArray())
23
24 '== 釋放資源
25 workbook = Nothing '== C#為 null
26 ms.Close()
27 ms.Dispose()
學通了,您的程式碼都便成「積木」
隨便組合都能千變萬化
沒學通,程式碼送給您,您都運作不起來
有沒有學通,不是靠聰明才智
而是靠自己學習的態度
我將思想傳授他人, 他人之所得,亦無損於我之所有;
猶如一人以我的燭火點燭,光亮與他同在,我卻不因此身處黑暗。----Thomas Jefferson
線上課程教學,遠距教學 (Web Form 約 51hr) https://dotblogs.com.tw/mis2000lab/2016/02/01/aspnet_online_learning_distance_education_VS2015
線上課程教學,遠距教學 (ASP.NET MVC 約 140hr) https://dotblogs.com.tw/mis2000lab/2018/08/14/ASPnet_MVC_Online_Learning_MIS2000Lab
寫信給我,不要私訊 -- mis2000lab (at) yahoo.com.tw 或 school (at) mis2000lab.net
(1) 第一天 ASP.NET MVC5 完整影片(5.5小時 / .NET 4.x版)免費試聽。影片 https://youtu.be/9spaHik87-A
(2) 第一天 ASP.NET Core MVC 完整影片(3小時 / .NET Core 6.0~8.0)免費試聽。影片 https://youtu.be/TSmwpT-Bx4I
[學員感言] mis2000lab課程評價 - ASP.NET MVC , WebForm 。 https://mis2000lab.medium.com/%E5%AD%B8%E5%93%A1%E6%84%9F%E8%A8%80-mis2000lab%E8%AA%B2%E7%A8%8B%E8%A9%95%E5%83%B9-asp-net-mvc-webform-77903ce9680b
ASP.NET遠距教學、線上課程(Web Form + MVC)。 第一天課程, "完整" 試聽。
......... facebook社團 https://www.facebook.com/mis2000lab ......................
......... YouTube (ASP.NET) 線上教學影片 https://www.youtube.com/channel/UC6IPPf6tvsNG8zX3u1LddvA/
Blog文章 "附的範例" 無法下載,請看 https://dotblogs.com.tw/mis2000lab/2016/03/14/2008_2015_mis2000lab_sample_download
請看我們的「售後服務」範圍(嚴格認定)。
......................................................................................................................................................
ASP.NET MVC => .NET Core MVC 線上教學 ...... 第一天課程 完整內容 "免費"讓您評估 / 試聽

[遠距教學、教學影片] ASP.NET (Web Form) 課程 上線了!MIS2000Lab.主講 事先錄好的影片,並非上課側錄! 觀看時,有如「一對一」面對面講課。
