C#Winform中DataGridView導(dǎo)出為Excel的實(shí)現(xiàn)示例_第1頁(yè)
C#Winform中DataGridView導(dǎo)出為Excel的實(shí)現(xiàn)示例_第2頁(yè)
C#Winform中DataGridView導(dǎo)出為Excel的實(shí)現(xiàn)示例_第3頁(yè)
C#Winform中DataGridView導(dǎo)出為Excel的實(shí)現(xiàn)示例_第4頁(yè)
C#Winform中DataGridView導(dǎo)出為Excel的實(shí)現(xiàn)示例_第5頁(yè)
已閱讀5頁(yè),還剩4頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

第C#Winform中DataGridView導(dǎo)出為Excel的實(shí)現(xiàn)示例目錄1、前言2、效果展示3、詳細(xì)步驟3.1添加NPOI和NPOI.Excel包3.2創(chuàng)建NPOIHelper類(lèi)3.3給畫(huà)面添加SaveFileDialog3.4引入命名空間3.5給按鈕添加click事件4、成功5、寫(xiě)在最后

1、前言

話不多說(shuō),跟著我的步驟保證你也能成功,下面直接開(kāi)始!

2、效果展示

導(dǎo)出前

導(dǎo)出后

3、詳細(xì)步驟

下面是詳細(xì)操作步驟,請(qǐng)跟著我的步伐,一步一步進(jìn)行操作,保證你能夠?qū)С龀晒Γ?/p>

3.1添加NPOI和NPOI.Excel包

首先請(qǐng)請(qǐng)確定你的vs已經(jīng)打開(kāi)了【解決方案資源管理器】,打開(kāi)步驟見(jiàn)下圖:

在資源管理器中找到【引用】,然后在【引用】上右鍵選擇【管理程序包】并點(diǎn)擊,如下圖:

在新打開(kāi)的窗口中點(diǎn)擊【瀏覽】并在搜索框中依次輸入NPOI和NPOI.Excel并進(jìn)行安裝,安裝按鈕位置如下圖:

待安裝完成再進(jìn)行下一步

3.2創(chuàng)建NPOIHelper類(lèi)

首先在資源管理器中選中你的項(xiàng)目,【右鍵】找到【添加】【類(lèi)】,具體如下圖:

創(chuàng)建一個(gè)名字為【NPOIHelper.cs】的類(lèi)并打開(kāi)

3.2.1導(dǎo)入命名空間

復(fù)制下面的代碼,覆蓋你自動(dòng)生成的命名空間

usingNPOI.HSSF.UserModel;

usingNPOI.SS.UserModel;

usingNPOI.XSSF.UserModel;

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Data;

usingSystem.IO;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Threading.Tasks;

usingSystem.Windows.Forms;

3.2.2插入代碼

將下面的代碼導(dǎo)入到下圖位置:

publicclassExcelUtility

///summary

///將excel導(dǎo)入到datatable

////summary

///paramname="filePath"excel路徑/param

///paramname="isColumnName"第一行是否是列名/param

///returns返回datatable/returns

publicstaticDataTableExcelToDataTable(stringfilePath,boolisColumnName)

DataTabledataTable=null;

FileStreamfs=null;

DataColumncolumn=null;

DataRowdataRow=null;

IWorkbookworkbook=null;

ISheetsheet=null;

IRowrow=null;

ICellcell=null;

intstartRow=0;

using(fs=File.OpenRead(filePath))

//版本后綴控制

if(filePath.IndexOf(".xlsx")0)

workbook=newXSSFWorkbook(fs);

//版本后綴控制

elseif(filePath.IndexOf(".xls")0)

workbook=newHSSFWorkbook(fs);

if(workbook!=null)

sheet=workbook.GetSheetAt(0);//讀取第一個(gè)sheet,當(dāng)然也可以循環(huán)讀取每個(gè)sheet

dataTable=newDataTable();

if(sheet!=null)

introwCount=sheet.LastRowNum;//總行數(shù)

if(rowCount0)

IRowfirstRow=sheet.GetRow(0);//第一行

intcellCount=firstRow.LastCellNum;//列數(shù)

//構(gòu)建datatable的列

if(isColumnName)

startRow=1;//如果第一行是列名,則從第二行開(kāi)始讀取

for(inti=firstRow.FirstCellNum;icellCount;++i)

cell=firstRow.GetCell(i);

if(cell!=null)

if(cell.StringCellValue!=null)

column=newDataColumn(cell.StringCellValue);

dataTable.Columns.Add(column);

else

for(inti=firstRow.FirstCellNum;icellCount;++i)

column=newDataColumn("column"+(i+1));

dataTable.Columns.Add(column);

//填充行

for(inti=startRow;i=rowCount;++i)

row=sheet.GetRow(i);

if(row==null)continue;

dataRow=dataTable.NewRow();

for(intj=row.FirstCellNum;jcellCount;++j)

cell=row.GetCell(j);

if(cell==null)

dataRow[j]="";

else

//CellType(Unknown=-1,Numeric=0,String=1,Formula=2,Blank=3,Boolean=4,Error=5,)

switch(cell.CellType)

caseCellType.Blank:

dataRow[j]="";

break;

caseCellType.Numeric:

shortformat=cell.CellStyle.DataFormat;

//對(duì)時(shí)間格式(2015.12.5、2015/12/5、2015-12-5等)的處理

if(format==14||format==31||format==57||format==58)

dataRow[j]=cell.DateCellValue;

else

dataRow[j]=cell.NumericCellValue;

break;

caseCellType.String:

dataRow[j]=cell.StringCellValue;

break;

dataTable.Rows.Add(dataRow);

returndataTable;

catch(Exception)

if(fs!=null)

fs.Close();

returnnull;

publicstaticboolDataTableToExcel(DataTabledt,stringtxtPath)

boolresult=false;

IWorkbookworkbook=null;

FileStreamfs=null;

IRowrow=null;

ISheetsheet=null;

ICellcell=null;

if(dt!=nulldt.Rows.Count0)

workbook=newHSSFWorkbook();

sheet=workbook.CreateSheet("Sheet0");//創(chuàng)建一個(gè)名稱(chēng)為Sheet0的表

introwCount=dt.Rows.Count;//行數(shù)

intcolumnCount=dt.Columns.Count;//列數(shù)

//設(shè)置列頭

row=sheet.CreateRow(0);//excel第一行設(shè)為列頭

for(intc=0;ccolumnCount;c++)

cell=row.CreateCell(c);

cell.SetCellValue(dt.Columns[c].ColumnName);

//設(shè)置每行每列的單元格,

for(inti=0;irowCount;i++)

row=sheet.CreateRow(i+1);

for(intj=0;jcolumnCount;j++)

cell=row.CreateCell(j);//excel第二行開(kāi)始寫(xiě)入數(shù)據(jù)

cell.SetCellValue(dt.Rows[i][j].ToString());

using(fs=File.OpenWrite(txtPath))

workbook.Write(fs);//向打開(kāi)的這個(gè)xls文件中寫(xiě)入數(shù)據(jù)

result=true;

MessageBox.Show("導(dǎo)出成功");

returnresult;

catch(Exception)

if(fs!=null)

fs.Close();

returnfalse;

3.3給畫(huà)面添加SaveFileDialog

首先在工具箱里找到SaveFileDialog(如果不知到工具箱在哪的可以點(diǎn)擊上方的【視圖】【工具箱】)

點(diǎn)住SaveFileDialog拖入頁(yè)面中效果如上圖所示

3.4引入命名空間

將下方命名空間引入到你按鈕所在的cs中(主要是3、4)

usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Threading.Tasks;

usingSystem.Windows.Forms;

usingSystem.Data.OleDb;

3.5給按鈕添加click事件

給按鈕添加click事件(直接雙擊按鈕即可),并在click函數(shù)中插入以下代碼

//打開(kāi)文件對(duì)話框,導(dǎo)出文件

SaveFileDialogsaveFileDialog1=newSaveFileDialog();

saveFileDialog1.Title="保存文件";

saveFileDialog1.Filter="Excel文件(*.xls)|*.xls|Excel文件(*.xlsx)|*.xlsx|所有文件(*.*)|*.*";

saveFileDialog1.FileName="用戶信息.xls";//設(shè)置默認(rèn)另存為的名字

if(this.saveFileDialog1.ShowDialog()==DialogResult.OK)

stringtxtPath=this.saveFileDialog1.FileName;

stringsql="selectIDasID,UserNameas用戶名,LoginAccountas賬號(hào),UserPoweras用戶權(quán)限,Founderas創(chuàng)建者,Addtimeas創(chuàng)建日期,Activestateas狀態(tài)fromUserData";

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論