C#WinFrom導(dǎo)出Excel過程解析
這篇文章主要介紹了C#WinFrom導(dǎo)出Excel過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
采用的是以DataGridView的形式導(dǎo)出,使用NPOI.dll
1.由于使用的是DataGridView,所以類需要?jiǎng)?chuàng)建在From的Project下,DLL導(dǎo)入NPOI
2.代碼如下
ExportExcel
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using NPOI.SS.UserModel; //NPOI using NPOI.HSSF.Util; //NPOI using NPOI.HSSF.UserModel; //NPOI using NPOI.XSSF.UserModel; //NPOI using System.IO; namespace ESMT { public class ExportExcel { /// <summary> /// /// </summary> /// <param name="grdview">數(shù)據(jù)表</param> /// <param name="sheetName">工作簿名字</param> /// <param name="FilePath">文件路徑</param> /// <param name="columnTitle">列頭</param> public void ExportToExcel(DataGridView grdview, string sheetName, string FilePath, string[] columnTitle) { //不允許dataGridView顯示添加行,負(fù)責(zé)導(dǎo)出時(shí)會(huì)報(bào)最后一行未實(shí)例化錯(cuò)誤 grdview.AllowUserToAddRows = false; HSSFWorkbook workbook = new HSSFWorkbook(); ISheet sheet = workbook.CreateSheet(sheetName);//創(chuàng)建工作簿 //設(shè)置表頭 IRow headerRow = sheet.CreateRow(0);//創(chuàng)建第一行 headerRow.HeightInPoints = 40; headerRow.CreateCell(0).SetCellValue("出庫(kù)表單");//單元格賦值 ICellStyle headStyle = workbook.CreateCellStyle(); headStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;//格式居中 IFont font = workbook.CreateFont(); font.Boldweight = 500; font.FontHeightInPoints = 20; headStyle.SetFont(font); headerRow.GetCell(0).CellStyle = headStyle; sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, grdview.ColumnCount - 2));//單元格合并 最后個(gè)參數(shù)是合并個(gè)數(shù) IRow headerRow2 = sheet.CreateRow(1);//創(chuàng)建第二行列頭 ICellStyle headStyle2 = workbook.CreateCellStyle(); headStyle2.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; IFont font2 = workbook.CreateFont(); font2.FontHeightInPoints = 10; font2.Boldweight = 700; headStyle2.SetFont(font2); for (int l = 0; l < grdview.ColumnCount - 1; l++) //列頭填值 { headerRow2.CreateCell(l).SetCellValue(columnTitle[l]); headerRow2.GetCell(l).CellStyle = headStyle2; } //設(shè)置列寬 for (int l = 0; l < grdview.Columns.Count; l++) { sheet.DefaultColumnWidth = 15; } //填寫內(nèi)容 for (int i = 0; i < grdview.Rows.Count; i++) { IRow row = sheet.CreateRow(i + 2); for (int j = 1; j < grdview.Columns.Count; j++) { row.CreateCell(j - 1, CellType.String).SetCellValue(grdview.Rows[i].Cells[j].Value.ToString());//j-1表示哪個(gè)單元格 } } using (FileStream stream = File.OpenWrite(FilePath))//創(chuàng)建Excel并寫入數(shù)據(jù) { workbook.Write(stream); stream.Close(); } GC.Collect(); } } }
PS:openwtrie 打開或者創(chuàng)建新的文件寫入
3.From窗口點(diǎn)擊導(dǎo)出按鈕
導(dǎo)出按鈕
string[] columnTitle = { "序號(hào)", "倉(cāng)位", "Facility", "供應(yīng)商料號(hào)", "料號(hào)", "料卷ID", "料卷數(shù)量", "儲(chǔ)位號(hào)", "Date Code/Lot", "生產(chǎn)日期", "供應(yīng)商編碼", "入倉(cāng)時(shí)間" }; string localFilePath = "";// fileNameExt, newFileName, FilePath; SaveFileDialog sfd = new SaveFileDialog();//保存文件窗口 //設(shè)置文件類型 sfd.Filter = "Excel(97-2003)|*.xls";//保存類型為EXCEL //保存對(duì)話框是否記憶上次打開的目錄 sfd.RestoreDirectory = true; //點(diǎn)了保存按鈕進(jìn)入 if (sfd.ShowDialog() == DialogResult.OK) { localFilePath = sfd.FileName.ToString(); //獲得文件路徑 ex.ExportToExcel(grdData, "出庫(kù)表單", localFilePath, columnTitle); }
通過以上三步,完成點(diǎn)擊導(dǎo)出按鈕,后選擇保存位置并命名,調(diào)用EportExcel方法完成導(dǎo)出Excel。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
欄 目:C#教程
下一篇:unity實(shí)現(xiàn)場(chǎng)景切換進(jìn)度條顯示
本文標(biāo)題:C#WinFrom導(dǎo)出Excel過程解析
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/4609.html
您可能感興趣的文章
- 01-10C#導(dǎo)出網(wǎng)站功能實(shí)例代碼講解
- 01-10C#讀取Excel的三種方式以及比較分析
- 01-10C#定制Excel界面并實(shí)現(xiàn)與數(shù)據(jù)庫(kù)交互的方法
- 01-10C#使用Aspose.Cells控件讀取Excel
- 01-10C#實(shí)現(xiàn)Excel動(dòng)態(tài)生成PivotTable
- 01-10C#的Excel導(dǎo)入、導(dǎo)出
- 01-10C#使用oledb導(dǎo)出數(shù)據(jù)到excel的方法
- 01-10C#.net編程創(chuàng)建Access文件和Excel文件的方法詳解
- 01-10C#控制Excel Sheet使其自適應(yīng)頁(yè)寬與列寬的方法
- 01-10C#實(shí)現(xiàn)DataSet內(nèi)數(shù)據(jù)轉(zhuǎn)化為Excel和Word文件的通用類完整實(shí)例


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-10delphi制作wav文件的方法
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10C#中split用法實(shí)例總結(jié)