1.创建winform窗体,添加导入导出按钮,添加gridControl(需要下载dev插件)没有的话添加DatagridView也可以。如下图所示:
2.开始写代码,首先在项目的引用文件下添加NPOI文件(需要下载,之前在哪下载的忘记了,可以百度瞅瞅)。
3.然后引用其命名空间
4.限制其可读取的excel类型,先添加一个命名空间IO。
5.在导入按钮中书写代码
DataTable Table()
{
DataTable d = new DataTable();
d.Columns.Add("name");
d.Columns.Add("sex");
d.Columns.Add("phone");
d.Columns.Add("address");
return d;
}
//导入按钮
private void btnImport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
//清空表头
gridView1.Columns.Clear();
//清空数据集
gridControl1.DataSource = null;
string _msg = "";
try
{
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string _filePath = openFileDialog1.FileName;
if (_filePath != "")
{
using (FileStream fs = new FileStream(_filePath, FileMode.Open, FileAccess.Read))
{
IWorkbook wk = GetWorkBook(fs, _filePath);
DataTable dt = Table();
ISheet hsheet = wk.GetSheetAt(0);
int _lastRow = hsheet.LastRowNum;
IRow _row = hsheet.GetRow(1);
for (int i = 1; i <= _lastRow; i++)
{
_row = hsheet.GetRow(i);
if (_row.GetCell(0) == null)
{
break;
}
_row.GetCell(0).SetCellType(CellType.String);
string name = _row.GetCell(0).StringCellValue; //姓名
if (name == "") ;
{
break;
}
_row.GetCell(1).SetCellType(CellType.String);
string sex = _row.GetCell(1).StringCellValue;//性别
_row.GetCell(2).SetCellType(CellType.String);
string phone = _row.GetCell(2).StringCellValue; //电话号
_row.GetCell(3).SetCellType(CellType.String);
string address = _row.GetCell(3).StringCellValue;//家庭住址
DataRow dr = dt.NewRow();
dr["name"] = name;
dr["sex"] = sex;
dr["phone"] = phone;
dr["address"] = address;
dt.Rows.Add(dr);
}
gridControl1.DataSource = dt;
gridView1.BestFitColumns();
}
_msg = "共导入" + (gridView1.RowCount).ToString() + "笔记录!";
}
}
}
catch (Exception es)
{
_msg = es.Message;
}
finally
{
if (_msg != "")
{
MessageBox.Show(_msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
6.现在我们需要写一个类来封装导出excel的方法,新建一个ExportToExcel类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DevExpress.XtraPivotGrid;
using DevExpress.XtraGrid;
using DevExpress.XtraPrinting;
using System.Windows.Forms;
namespace 窗体导入导出文件
{
class ExportToExcel
{
public static void GridToXlsx(GridControl gctr, string filename)
{
SaveFileDialog saveFile = new SaveFileDialog();
saveFile.Filter = "Excel文件(.xlsx)|*.xlsx";
saveFile.FileName = filename;
if (saveFile.ShowDialog() == DialogResult.OK)
{
gctr.ExportToXlsx(saveFile.FileName);
MessageBox.Show("成功导出Excel文件!");
}
}
public static void GridToXls(GridControl gctr, string filename)
{
SaveFileDialog saveFile = new SaveFileDialog();
saveFile.Filter = "Excel文件(*.xls)|*.xls";
saveFile.FileName = filename;
if (saveFile.ShowDialog() == DialogResult.OK)
{
gctr.ExportToXlsx(saveFile.FileName);
MessageBox.Show("成功导出Excel文件!");
}
}
public static void GridToExcel(GridControl gctr, string filename)
{
SaveFileDialog saveFile = new SaveFileDialog();
saveFile.Filter = "Excel2003(*.xls)|*.xls|Excel2007(*.xlsx)|*.xlsx";
saveFile.FileName = filename;
if (saveFile.ShowDialog() == DialogResult.OK)
{
if (saveFile.FilterIndex == 1)
{
gctr.ExportToXlsx(saveFile.FileName);
}
else if (saveFile.FilterIndex == 2)
{
gctr.ExportToXlsx(saveFile.FileName);
MessageBox.Show("成功导出Excel文件!");
}
}
}
}
}
7.导入按钮书写代码
//导出按钮
private void btnExport_ItemClick(objectsenderDevExpress.XtraBars.ItemClickEventArgs e)
{
ExportToExcel.GridToExcel(gridControl1, "学生信息表");
}