C# dataGridView 读取Excel 表格内容 报错:对象不能从DBNUll转换为其他类型 显示报错后,点确定,程序还继续执行,怎么把报错去掉 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 没有内容的单元格,如果你使用 ADO.NET 来读取 Excel,返回的当然就是 DBNull。你从原始的 Excel 就能看到哪些单元格可能没填写内容。 数据没对应上。参考:https://blog.csdn.net/C_gyl/article/details/85067599 加个判断呗if(dgvcells.value.equals(dbnull.value)) {....} 给你发一个我现在用的 ,增加引用Microsoft.Office.Interop.Excel.dll using System.Data.OleDb; using System.Collections; using Excel = Microsoft.Office.Interop.Excel; /// <summary> /// 根据文件名称filaname,表名称获得数据sheetname /// </summary> /// <param name="filename"></param> /// <param name="sheetname"></param> /// <returns></returns> public DataSet GetData(string filename,string sheetname) { string connStr = ""; //string fileType = System.IO.Path.GetExtension("d:/test.xls"); string fileType = System.IO.Path.GetExtension(filename); if (string.IsNullOrEmpty(fileType)) return null; if (fileType == ".xls") connStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filename + ";" + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\""; else connStr = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + filename + ";" + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\""; string sql_F = "Select * FROM [{0}]"; OleDbConnection conn = null; OleDbDataAdapter da = null; //DataTable dtSheetName = null; da = new OleDbDataAdapter(); DataSet ds = new DataSet(); try { // 初始化连接,并打开 conn = new OleDbConnection(connStr); conn.Open(); // 获取数据源的表定义元数据 da.SelectCommand = new OleDbCommand(string.Format(sql_F, sheetname), conn); DataSet dsItem = new DataSet(); da.Fill(dsItem, sheetname); ds.Tables.Add(dsItem.Tables[0].Copy()); } catch { } finally { // 关闭连接 if (conn.State == ConnectionState.Open) { conn.Close(); da.Dispose(); conn.Dispose(); } } dataGridView1.DataSource = ds.Tables[0]; 两条C++语句转C# 如何控制管脚电平 中文语音识别的问题?? 要接项目了,大家来帮我估个价! cookies 乱码问题 FORM1激活FORM2,如何关闭FORM2退出整个程序??? FTP的api函数FtpFindFirstFile的问题。 c#中的 main函数 水晶报表为什么每次都提示登陆数据库呀?兄弟们帮帮忙吧! 如何得到指针(鼠标)的当前位置?? C#代码中实现两个表(DataTable)的关联查询(JOIN) 问题 C# SuperTabControl设置tab text中文乱码,这个如何解决,App.config是utf-8
参考:https://blog.csdn.net/C_gyl/article/details/85067599
if(dgvcells.value.equals(dbnull.value))
{....}
using System.Data.OleDb;
using System.Collections;
using Excel = Microsoft.Office.Interop.Excel;
/// <summary>
/// 根据文件名称filaname,表名称获得数据sheetname
/// </summary>
/// <param name="filename"></param>
/// <param name="sheetname"></param>
/// <returns></returns>
public DataSet GetData(string filename,string sheetname)
{
string connStr = "";
//string fileType = System.IO.Path.GetExtension("d:/test.xls");
string fileType = System.IO.Path.GetExtension(filename);
if (string.IsNullOrEmpty(fileType)) return null; if (fileType == ".xls")
connStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filename + ";" + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
else
connStr = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + filename + ";" + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\"";
string sql_F = "Select * FROM [{0}]"; OleDbConnection conn = null;
OleDbDataAdapter da = null;
//DataTable dtSheetName = null; da = new OleDbDataAdapter();
DataSet ds = new DataSet();
try
{
// 初始化连接,并打开
conn = new OleDbConnection(connStr);
conn.Open(); // 获取数据源的表定义元数据
da.SelectCommand = new OleDbCommand(string.Format(sql_F, sheetname), conn);
DataSet dsItem = new DataSet();
da.Fill(dsItem, sheetname); ds.Tables.Add(dsItem.Tables[0].Copy());
}
catch
{
}
finally
{
// 关闭连接
if (conn.State == ConnectionState.Open)
{
conn.Close();
da.Dispose();
conn.Dispose();
}
}
dataGridView1.DataSource = ds.Tables[0];