資源簡介
C# WinForm 工作中遇到一個需要將界面表格數據按照設定的格式[表頭|列表|表尾]導出到Excel文件,因為格式繁多一個個固定代碼編寫很不現實,網上找了很久都沒有相關的功能實例,于是就加班自己動手寫了一個通用的導出實例,已應用到代碼中?,F為方便廣大開發者遍歷特上傳通用精簡版分享給大家 如有優化建議和方向的同志可以加Q:398719557 一起交流學習進步 待解決問題: 1.界面設計時合并單元格問題(導出已合并)方便編輯模板 2.導出單元格背景色問題
完整版還有自動反射字段中文名稱方便客戶自己編輯 時間匆忙就懶得分離代碼上傳 了 原理很簡單 字段自定義屬性[PropertyDescriptor] 然后反射就好了
完整版還有自動反射字段中文名稱方便客戶自己編輯 時間匆忙就懶得分離代碼上傳 了 原理很簡單 字段自定義屬性[PropertyDescriptor] 然后反射就好了

代碼片段和文件信息
using?ExcelUtility;
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.IO;
using?System.Linq;
using?System.Text;
using?System.Windows.Forms;
namespace?ExcelDome
{
????public?partial?class?Dome?:?Form
????{
????????///?
????????///?表格數據
????????///?
????????public?Listject>?Data?=?new?Listject>();
????????///?
????????///?初始化
????????///?
????????public?Dome()
????????{
????????????InitializeComponent();
????????}
????????///?
????????///?關閉按鈕被單擊事件
????????///?
????????///?
????????///?
????????private?void?Ts_Close_Click(object?sender?EventArgs?e)
????????{
????????????this.Close();
????????}
????????///?
????????///?模板按鈕被單擊事件
????????///?
????????///?
????????///?
????????private?void?Ts_EditExport_Click(object?sender?EventArgs?e)
????????{
????????????var?DataName?=?(sender?as?ToolStripMenuItem).Text;
????????????//模板文件名
????????????var?FileName?=?String.Format(“{0}\\{1}\\{2}“?Path.GetDirectoryName(Application.ExecutablePath)?“ExcelTemplate“?String.Format(“{0}.ETD“?DataName));
????????????var?CustomData?=?new?Dictionary();
????????????//自己添加需要的自定義數據或者通過對象轉換
????????????CustomData.Add(“當前時間“?DateTime.Now.ToString(“yyyy-MM-dd?HH:mm:ss“));
????????????var?ListData?=?new?List>();
????????????//將表格數據轉換為需要的格式
????????????Data.ForEach(x?=>
????????????{
????????????????ListData.Add(x.ToDictionary());
????????????});
????????????using?(var?F?=?new?ExcelTemplateDesign(FileName?CustomData?ListData))
????????????{
????????????????F.ShowDialog().Equals(DialogResult.OK);
????????????}
????????}
????????///?
????????///?導出按鈕被單擊事件
????????///?
????????///?
????????///?
????????private?void?Ts_Export_Click(object?sender?EventArgs?e)
????????{
????????????var?DataName?=?(sender?as?ToolStripMenuItem).Text;
????????????var?FileName?=?String.Format(“{0}\\{1}\\{2}“?Path.GetDirectoryName(Application.ExecutablePath)?“ExcelTemplate“?String.Format(“{0}.ETD“?DataName));
????????????if?(!File.Exists(FileName))?throw?new?Exception(“模板文件不存在請先編輯導出模板!“);
????????????using?(FolderBrowserDialog?Fbd?=?new?FolderBrowserDialog()?{?Description?=?“請選擇導出文件保存目錄“?})
????????????{
????????????????if?(Fbd.ShowDialog().Equals(DialogResult.OK)?&&?!String.IsNullOrEmpty(Fbd.SelectedPath))
????????????????{
????????????????????var?SavePath?=?Fbd.SelectedPath;
????????????????????var?Info?=?RbwExport.GetExcelTemplateInfo(FileName);
????????????????????var?CustomData?=?new?Dictionary();
????????????????????//自己添加需要的自定義數據或者通過對象轉換
????????????????????CustomData.Add(“當前時間“?DateTime.Now.ToString(“yyyy-MM-dd?HH:mm:
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
????..A..H.?????65536??2019-12-20?17:03??ExcelDome\.vs\ExcelDome\v15\.suo
?????文件??????????0??2019-12-20?15:56??ExcelDome\.vs\ExcelDome\v15\Server\sqlite3\db.lock
?????文件?????876544??2019-12-20?17:03??ExcelDome\.vs\ExcelDome\v15\Server\sqlite3\storage.ide
?????文件??????22528??2019-12-20?16:59??ExcelDome\ExcelDome\bin\Debug\ExcelDome.exe
?????文件??????24064??2019-12-20?16:59??ExcelDome\ExcelDome\bin\Debug\ExcelDome.pdb
?????文件???????1655??2019-12-20?17:01??ExcelDome\ExcelDome\bin\Debug\ExcelTemplate\自定義[格式一].ETD
?????文件???????1805??2019-12-20?17:03??ExcelDome\ExcelDome\bin\Debug\ExcelTemplate\自定義[格式二].ETD
?????文件??????71168??2019-12-20?16:37??ExcelDome\ExcelDome\bin\Debug\ExcelUtility.dll
?????文件??????71168??2019-12-20?16:37??ExcelDome\ExcelDome\bin\Debug\ExcelUtility.pdb
?????文件????1679360??2017-11-29?15:09??ExcelDome\ExcelDome\bin\Debug\NPOI.dll
?????文件?????536064??2017-11-29?15:21??ExcelDome\ExcelDome\bin\Debug\NPOI.OOxm
?????文件??????91136??2017-11-29?15:21??ExcelDome\ExcelDome\bin\Debug\NPOI.Openxm
?????文件????2121728??2017-11-29?15:21??ExcelDome\ExcelDome\bin\Debug\NPOI.Openxm
?????文件????2254144??2017-11-15?09:40??ExcelDome\ExcelDome\bin\Debug\NPOI.xm
?????文件???????4923??2019-12-20?16:58??ExcelDome\ExcelDome\Demo.cs
?????文件???????3602??2019-12-20?16:52??ExcelDome\ExcelDome\ExcelDome.csproj
?????文件??????11406??2019-12-20?16:52??ExcelDome\ExcelDome\Form1.Designer.cs
?????文件??????14864??2019-12-20?16:52??ExcelDome\ExcelDome\Form1.resx
?????文件????????864??2019-12-20?15:56??ExcelDome\ExcelDome\obj\Debug\DesignTimeResolveAssemblyReferences.cache
?????文件???????6942??2019-12-20?16:46??ExcelDome\ExcelDome\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件??????????0??2019-12-20?16:59??ExcelDome\ExcelDome\obj\Debug\ExcelDome.csproj.CopyComplete
?????文件?????????42??2019-12-20?16:46??ExcelDome\ExcelDome\obj\Debug\ExcelDome.csproj.CoreCompileInputs.cache
?????文件???????1327??2019-12-20?16:52??ExcelDome\ExcelDome\obj\Debug\ExcelDome.csproj.FileListAbsolute.txt
?????文件???????1012??2019-12-20?16:53??ExcelDome\ExcelDome\obj\Debug\ExcelDome.csproj.GenerateResource.cache
?????文件??????67712??2019-12-20?16:52??ExcelDome\ExcelDome\obj\Debug\ExcelDome.csprojResolveAssemblyReference.cache
?????文件???????5580??2019-12-20?16:53??ExcelDome\ExcelDome\obj\Debug\ExcelDome.Dome.resources
?????文件??????22528??2019-12-20?16:59??ExcelDome\ExcelDome\obj\Debug\ExcelDome.exe
?????文件??????24064??2019-12-20?16:59??ExcelDome\ExcelDome\obj\Debug\ExcelDome.pdb
?????文件????????180??2019-12-20?16:52??ExcelDome\ExcelDome\obj\Debug\ExcelDome.Properties.Resources.resources
?????文件????????489??2019-12-20?16:06??ExcelDome\ExcelDome\Program.cs
............此處省略63個文件信息
- 上一篇:C# 學生信息管理系統(SQLite)
- 下一篇:C#程序防止反編譯工具
評論
共有 條評論