首页 > 其他分享 >npoi快速读入到DGV

npoi快速读入到DGV

时间:2023-10-19 16:55:26浏览次数:33  
标签:sheet GetRow npoi DGV dataGridView1 读入 new openFileDialog Columns

 

//nuget导入npoi我就不说了。。。

 

private void button1_Click(object sender, EventArgs e)
{
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//交互 ->不需要拖入控件
string strFileName = "";
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = @"c:\"; //注意这里写路径时要用c而不是c:
openFileDialog.Filter = "Excel files(*.xlsx)|*.xlsx";
openFileDialog.RestoreDirectory = true;
openFileDialog.FilterIndex = 1;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
strFileName = openFileDialog.FileName; //路径+文件(含扩展名)
//strName = openFileDialog.SafeFileName; //文件(含扩展名)
}
else { return; }

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//设置dataGridView
//基本设置
dataGridView1.Columns.Clear();
dataGridView1.DataSource = null;
dataGridView1.ColumnCount = 3; //dataGridView1.Rows.Count = 10;只读
dataGridView1.ColumnHeadersVisible = true;
//列头行高
dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing; //列头行高
dataGridView1.ColumnHeadersHeight = 22; //列头行高
dataGridView1.RowTemplate.Height = 22; //数据格子行高
//列头文字,字体,颜色,居中
dataGridView1.EnableHeadersVisualStyles = false;//这样就可以使用当前的主题的样式了,这句话十分关键!
//dataGridView1.ColumnHeadersDefaultCellStyle.Font = new Font("宋体",12, FontStyle.Bold); //dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = Color.Purple;
DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle(); // Set the column header style.
columnHeaderStyle.BackColor = Color.Beige;
columnHeaderStyle.ForeColor = Color.Purple;
columnHeaderStyle.Font = new Font("宋体", 9); //, FontStyle..Bold);
columnHeaderStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; //, FontStyle..Bold); 文字居中显示
dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle;
//固定列
//dataGridView1.Columns[2].Frozen = true;
dataGridView1.AllowUserToAddRows = false; //禁止在末尾追加新行
dataGridView1.RowHeadersVisible = false; //行头可见
dataGridView1.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable; //禁止排序:NotSortable/排序:Automatic
//dataGridView1.AllowUserToResizeColumns = false; // 禁止用户改变DataGridView1的所有列的列宽
dataGridView1.AllowUserToResizeRows = false;//禁止用户改变DataGridView1の所有行的行高 ->但是可以通过 DataGridViewColumn.Width 或者 DataGridViewRow.Height 属性设定列宽和行高。
//只读(编辑)+光条显示
dataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically; //.EditOnEnter; //设定为编辑模式
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView1.MultiSelect = false;

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//列名设置
dataGridView1.Columns[0].Name = "班级";
dataGridView1.Columns[0].Width = 35;
dataGridView1.Columns[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; //控制对齐

dataGridView1.Columns[1].Name = "姓名";
dataGridView1.Columns[1].Width = 95;
dataGridView1.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;

//dataGridView1.Columns[2].Name = "车号";
//dataGridView1.Columns[2].Width = 105;
//dataGridView1.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//读入Excel
try
{
//打开文件
FileStream fileStream = new FileStream(strFileName, FileMode.Open);

IWorkbook workbook = null; //新建IWorkbook对象
if (strFileName.IndexOf(".xlsx") > 0) // 2007版本
{ workbook = new XSSFWorkbook(fileStream); } //xlsx数据读入workbook }
else if (strFileName.IndexOf(".xls") > 0) // 2003版本
{ workbook = new HSSFWorkbook(fileStream); } //xls数据读入workbook

ISheet sheet = workbook.GetSheetAt(0); //获取第一个工作表
IRow row;// = sheet.GetRow(0); //新建当前工作表行数据

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//取数据
dataGridView1.Rows.Clear();
string strStuGrade, strStuName, strStuTarget;
for (int i = 0; i < sheet.LastRowNum; i++) //遍历所有的行(从0开始)
{
strStuGrade = ""; strStuName = ""; strStuTarget = "";
if (sheet.GetRow(i + 1).GetCell(0) != null) { strStuGrade = sheet.GetRow(i + 1).GetCell(0).ToString(); }
if (sheet.GetRow(i + 1).GetCell(1) != null) { strStuName = sheet.GetRow(i + 1).GetCell(1).ToString(); }
if (sheet.GetRow(i + 1).GetCell(2) != null)
{

strStuTarget = sheet.GetRow(i + 1).GetCell(2).ToString();
if (strStuTarget.Contains("\n"))
{
strStuTarget = strStuTarget.Replace("\n", "");
}
}

//if (i>67) { MessageBox.Show("1111"); }
if (strStuGrade == "")
{
break;
}
dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells[0].Value = strStuGrade;
dataGridView1.Rows[i].Cells[1].Value = strStuName;
dataGridView1.Rows[i].Cells[2].Value = strStuTarget;
} //for i

sheet = null;
workbook = null;
fileStream.Close();
fileStream.Dispose();
}
catch (Exception ex)
{
throw ex;
}
}

标签:sheet,GetRow,npoi,DGV,dataGridView1,读入,new,openFileDialog,Columns
From: https://www.cnblogs.com/wsyyzy/p/17775110.html

相关文章

  • 关于 npoi 的 DateUtil.IsCellDateFormatted(cell) 为true,取cell.DateCellValue却报
    NPOI中数字和日期都是NUMERIC类型的,这里对其进行判断是否是日期类型所以当 DateUtil.IsCellDateFormatted为true时,理论是应该可以取到  cell.DateCellValue但实际上,cell.DateCellValue可能会报异常,而取 cell.NumericCellValue却是正常的,HSSFWorkbook是excel2007以前......
  • 使用NPOI修改Excel数据
    读取excel文件阿松大读取excel文件读取excel文件读取excel文件publicvoidSignIn(){Useruser=_userService.GetUserByName(Username);if(user!=null&&user.Password==Password){#region记住登录信息//将当前的配置序列化为j......
  • pinpoint agent无法启动问题
    问题描述启动命令/usr/local/java/jdk1.8.0_281/bin/java-jar-javaagent:/pinpoint/pinpoint-agent-2.2.1/pinpoint-bootstrap-2.2.1.jar-Dpinpoint.agentId=153.18-2221-Dpinpoint.applicationName=sample/data/app.jar之前没有问题,但是在现场环境,启动以后报错,找不到主......
  • C语言使用%d读入字符会发生什么
    指定了%d来读取一个整数,scanf()函数开始每次读取一个输入字符,它跳过空白字符(包含空格、制表符和换行符)直到遇到一个非空白字符,并期望发现一个数字字符或者一个符号(+或者-)。如果发现一个数字或符号,那么就保存并读取下一个字符;如果接下来的字符是一个数字,再保存,继续读取。如此持......
  • C#使用NPOI读取模板生成EXCEL
     C#使用NPOI读取模板生成EXCELstringcurrentDirectory=System.AppDomain.CurrentDomain.BaseDirectory;//读取Excel模板文件FileStreamfs=newFileStream(currentDirectory+"BoxPackinglist.xlsx",FileMode.Open,FileA......
  • linux 中awk命令指定读入分隔符
     001、-F指定[root@pc1test01]#lsa.txt[root@pc1test01]#cata.txta:b:c3:8:kf:6:3[root@pc1test01]#awk-F":"'{print$1}'a.txta3f 002、-vFS变量指定[root@pc1test01]#lsa.txt[root@pc1test01]#cata.txta:b:c3:8:k......
  • 关于读入速度的小实验
    2023-08-2911:13:16有人跟我说去同步的cin比普通快读要快,我觉得有点邪门,所以做了一个小比较,然后果不其然又发现了奇怪的事情。用以下代码测试:注:输入数据\(n=5\times10^7,|w|\le2^{64}-1\),由于是在学校机房老年机跑,所以测试结果可能与在其他地方测试相差较大,但是基于同一......
  • java中如何用Scanner类读入单个字符
    Scanner没有直接读入单个字符的方法,next方法没办法读入空格符,因为Scanner以空格符作为输入完毕的标志importjava.util.Scanner;Scannerin=newScanner(System.in);Strings=in.nextLine();char[]chars=s.toArray();charc=chars[0];//c就是读入的单个字符......
  • Dotnet6 NPOI操作Excel基本操作总结
    背景需要对Excel进行读取和写入,目前使用Dotnet6开发环境,故直接使用。达到的效果:兼容.xls和.xlsx,识别行为空自动跳过,识别显示值,识别格式内容步骤Dotnet6Nuget安装NPOI,具体版本2.6.1,tips:搜索资料时,可能NPOI1与NPOI2可能有出入。使用方法获取相应文档对象......
  • .NET Core使用NPOI导出复杂Word详解
    前言:最近使用NPOI做了个导出Word文档的功能,关于使用.NETCore导出Word文档的方式有很多。最终我为什么选择了NPOI来实现了这个功能,首先是NPOI是一个开源,免费且容易上手的第三方框架(并且现在已支持.NETCore,GitHub源码地址:https://github.com/tonyqus/npoi)。因为之前使用NP......