NPOI,給指定的excle創建個下拉框驗證
先大致看下效果吧
Nuget 搜索 NPOI,一般出來的第一個就是,安裝NPOI基礎環境
1 using NPOI.HSSF.UserModel;
2 using NPOI.OpenXmlFormats.Spreadsheet;
3 using NPOI.SS.UserModel;
4 using NPOI.SS.Util;
5 using NPOI.XSSF.UserModel;
6 using System;
7 using System.Collections.Generic;
8 using System.IO;
9 using System.Linq;
10 using System.Text;
11 using System.Threading.Tasks;
12 using System.Windows;
13 using System.Windows.Controls;
14 using System.Windows.Data;
15 using System.Windows.Documents;
16 using System.Windows.Input;
17 using System.Windows.Media;
18 using System.Windows.Media.Imaging;
19 using System.Windows.Navigation;
20 using System.Windows.Shapes;
21 using Path = System.IO.Path;
22
23 namespace NPOIDemo
24 {
25 /// <summary>
26 /// MainWindow.xaml 的交互邏輯
27 /// </summary>
28 public partial class MainWindow : Window
29 {
30 public MainWindow()
31 {
32 InitializeComponent();
33
34 var ff = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "服務器導入模板.xlsx");
35 CreateCellDropDownList(
36 ff,
37 new string[] { "1111", "2222", "3333", "4444" },
38 1, 4, 3, 3
39 );
40 System.Diagnostics.Process.Start(ff);
41 }
42
43 /// <summary>
44 /// 給指定的excle添加個下拉框驗證
45 /// 備註:只能給未添加下拉框驗證的添加,追加的情況不理想
46 /// </summary>
47 /// <param name="exclepath">excle路徑</param>
48 /// <param name="dropDownConstraint">有效值集合</param>
49 public void CreateCellDropDownList(string exclepath, string[] dropDownlist, int firstRow = 0, int lastRow = 65535, int firstCol = 0, int lastCol = 256)
50 {
51 FileInfo file = new FileInfo(exclepath);
52 if (!file.Exists)
53 {
54 throw new FileNotFoundException("Excle文件不存在!");
55 }
56 if (dropDownlist == null || dropDownlist.Length == 0)
57 {
58 throw new ValueUnavailableException("有效值集合不允許爲空!");
59 }
60 if (firstRow < 0 || lastRow < 0 || firstCol < 0 || lastCol < 0)
61 {
62 throw new IndexOutOfRangeException("索引值下標有誤,從0開始!");
63 }
64 file.IsReadOnly = false;
65
66 IWorkbook workbook = null;
67
68 using (FileStream fs = File.Open(file.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
69 {
70 workbook = new XSSFWorkbook(fs);
71 }
72
73 ISheet sheet = workbook.GetSheetAt(0);
74
75 //設置默認值
76
77 for (int irow = firstRow, length = lastRow; irow <= length; irow++)
78 {
79 var row = sheet.GetRow(irow);
80 if (row == null)
81 {
82 continue;
83 }
84
85 for (int icell = firstCol, lengthc = lastCol; icell <= lengthc; icell++)
86 {
87 var cell = row.GetCell(icell);
88 if (cell == null)
89 {
90 continue;
91 }
92 cell.SetCellValue(dropDownlist.First());
93 }
94 }
95
96 //設置生成下拉框的行和列
97 CellRangeAddressList cellRegions = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol);
98
99 XSSFDataValidationHelper helper = new XSSFDataValidationHelper((XSSFSheet)sheet);
100 //啓用下拉驗證
101 var dropDownConstraint = helper.CreateExplicitListConstraint(dropDownlist);
102 IDataValidation dropDownValidation = helper.CreateValidation(dropDownConstraint, cellRegions);
103 sheet.AddValidationData(dropDownValidation);
104 sheet.ValidateMergedRegions();
105
106 var filenew = Path.Combine(file.DirectoryName, "filenew" + Path.GetExtension(file.Name));
107 using (FileStream fs = File.Open(filenew, FileMode.OpenOrCreate, FileAccess.ReadWrite))
108 {
109 workbook.Write(fs);
110 }
111
112 //因爲暫時無法解決編輯的情況,所以迂迴下,刪除原始再覆蓋下
113 File.Delete(file.FullName);
114 File.Move(filenew, file.FullName);
115 }
116 }
117 }
特此說明下:添加下拉框數據驗證在已經存在的情況不理想,後面生成的會失效,暫時沒找到解決辦法,只有每次生成的數據操作一個空數據驗證的模板