C#Winform中DataGridView导出为Excel的实现示例_第1页
C#Winform中DataGridView导出为Excel的实现示例_第2页
C#Winform中DataGridView导出为Excel的实现示例_第3页
C#Winform中DataGridView导出为Excel的实现示例_第4页
C#Winform中DataGridView导出为Excel的实现示例_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第C#Winform中DataGridView导出为Excel的实现示例目录1、前言2、效果展示3、详细步骤3.1添加NPOI和NPOI.Excel包3.2创建NPOIHelper类3.3给画面添加SaveFileDialog3.4引入命名空间3.5给按钮添加click事件4、成功5、写在最后

1、前言

话不多说,跟着我的步骤保证你也能成功,下面直接开始!

2、效果展示

导出前

导出后

3、详细步骤

下面是详细操作步骤,请跟着我的步伐,一步一步进行操作,保证你能够导出成功!

3.1添加NPOI和NPOI.Excel包

首先请请确定你的vs已经打开了【解决方案资源管理器】,打开步骤见下图:

在资源管理器中找到【引用】,然后在【引用】上右键选择【管理程序包】并点击,如下图:

在新打开的窗口中点击【浏览】并在搜索框中依次输入NPOI和NPOI.Excel并进行安装,安装按钮位置如下图:

待安装完成再进行下一步

3.2创建NPOIHelper类

首先在资源管理器中选中你的项目,【右键】找到【添加】【类】,具体如下图:

创建一个名字为【NPOIHelper.cs】的类并打开

3.2.1导入命名空间

复制下面的代码,覆盖你自动生成的命名空间

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插入代码

将下面的代码导入到下图位置:

publicclassExcelUtility

///summary

///将excel导入到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);//读取第一个sheet,当然也可以循环读取每个sheet

dataTable=newDataTable();

if(sheet!=null)

introwCount=sheet.LastRowNum;//总行数

if(rowCount0)

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

intcellCount=firstRow.LastCellNum;//列数

//构建datatable的列

if(isColumnName)

startRow=1;//如果第一行是列名,则从第二行开始读取

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;

//对时间格式(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");//创建一个名称为Sheet0的表

introwCount=dt.Rows.Count;//行数

intcolumnCount=dt.Columns.Count;//列数

//设置列头

row=sheet.CreateRow(0);//excel第一行设为列头

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

cell=row.CreateCell(c);

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

//设置每行每列的单元格,

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

row=sheet.CreateRow(i+1);

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

cell=row.CreateCell(j);//excel第二行开始写入数据

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

using(fs=File.OpenWrite(txtPath))

workbook.Write(fs);//向打开的这个xls文件中写入数据

result=true;

MessageBox.Show("导出成功");

returnresult;

catch(Exception)

if(fs!=null)

fs.Close();

returnfalse;

3.3给画面添加SaveFileDialog

首先在工具箱里找到SaveFileDialog(如果不知到工具箱在哪的可以点击上方的【视图】【工具箱】)

点住SaveFileDialog拖入页面中效果如上图所示

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函数中插入以下代码

//打开文件对话框,导出文件

SaveFileDialogsaveFileDialog1=newSaveFileDialog();

saveFileDialog1.Title="保存文件";

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

saveFileDialog1.FileName="用户信息.xls";//设置默认另存为的名字

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

stringtxtPath=this.saveFileDialog1.FileName;

stringsql="selectIDasID,UserNameas用户名,LoginAccountas账号,UserPoweras用户权限,Founderas创建者,Addtimeas创建日期,Activestateas状态fromUserData";

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论