从excel读入数据到交错数组,已实例化过,然后for (int i = 0; i < count[0]; i++)
{
p[0][i] = Convert.ToInt16(dataGridView1[0, i].Value);
x1[0][i] = Convert.ToDouble(dataGridView1[1, i].Value);
y1[0][i] = Convert.ToDouble(dataGridView1[2, i].Value);
x2[0][i] = (Convert.ToDouble(dataGridView1[3, i].Value));
y2[0][i] = (Convert.ToDouble(dataGridView1[4, i].Value)); }报错“对象不能从 DBNull 转换为其他类型”,请问各位大虾如何处理啊,谢谢谢谢
{
p[0][i] = Convert.ToInt16(dataGridView1[0, i].Value);
x1[0][i] = Convert.ToDouble(dataGridView1[1, i].Value);
y1[0][i] = Convert.ToDouble(dataGridView1[2, i].Value);
x2[0][i] = (Convert.ToDouble(dataGridView1[3, i].Value));
y2[0][i] = (Convert.ToDouble(dataGridView1[4, i].Value)); }报错“对象不能从 DBNull 转换为其他类型”,请问各位大虾如何处理啊,谢谢谢谢
using System;
using System.Collections.Generic;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
using System.Data.OleDb;
using System.Data;
using System.Windows.Forms;
namespace ExcelDivision
{
class ExcelSearch
{
/// <summary>
/// 查询类
/// </summary>
/// <param name="excelFile">excel文件完整路径</param>
public ExcelSearch(string excelFile)
{
this.excelFile = excelFile;
try
{
excelConString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}; Extended Properties=Excel 8.0", excelFile);
excelConnnection = new OleDbConnection(excelConString);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private string excelFile;
private string excelConString;
public OleDbConnection excelConnnection;
/// <summary>
/// 查询全部数据
/// </summary>
/// <returns></returns>
public DataTable getAllDate(string sheetName)
{
DataTable dt = null;
string sql = string.Format("select * from [{0}$] ", sheetName);
try
{
DataSet ds = new DataSet();
OleDbDataAdapter adapter = new OleDbDataAdapter(sql,excelConnnection);
adapter.Fill(ds);
dt = ds.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message); }
finally
{
excelConnnection.Close();
}
return dt;
}
/// <summary>
/// 获取excel中有多少列数据
/// </summary>
/// <param name="sheetName"></param>
/// <returns></returns>
public int columnCount(string sheetName)
{
int num = 0;
string sql = string.Format("select * from [{0}$] ", sheetName);
try
{
OleDbCommand command = new OleDbCommand(sql, excelConnnection);
excelConnnection.Open();
OleDbDataReader dr = command.ExecuteReader();
dr.Read();
num=dr.FieldCount;
dr.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message); }
finally
{
excelConnnection.Close();
}
return num;
}
/// <summary>
///按列分类查询
/// </summary>
/// <param name="sheetName">工作表名</param>
/// <param name="columnName">列名</param>
/// <param name="condition">条件</param>
/// <returns></returns>
public DataTable getDataForColumnAsCondition(string sheetName,string columnName, string condition)
{
DataTable dt = null;
string sql = string.Format("select * from [{0}$] where {1}='{2}'",sheetName,columnName,condition);
try
{
DataSet ds = new DataSet();
OleDbDataAdapter adapter = new OleDbDataAdapter(sql,excelConnnection);
adapter.Fill(ds);
dt = ds.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message); }
finally
{
excelConnnection.Close();
}
return dt;
}
/// <summary>
/// 获取某一列的值(无重复值)
/// </summary>
/// <param name="sheetName">工作表</param>
/// <param name="columnName">要获取列的名字</param>
/// <returns></returns>
public DataTable getDataColumnName(string sheetName,string columnName)
{
DataTable dt = null;
string sql = string.Format("select DISTINCT {1} from [{0}$] ", sheetName, columnName);
try
{
OleDbDataAdapter adapter = new OleDbDataAdapter(sql, excelConnnection);
DataSet ds = new DataSet();
adapter.Fill(ds);
dt = ds.Tables[0];
}
catch (Exception)
{
}
return dt;
} public List<string> getRow(string sheetName)
{
List<string> lst = null;
return lst;
}
/// <summary>
/// 获取指定单元格的文本
/// </summary>
/// <param name="sheetName"></param>
/// <param name="row"></param>
/// <param name="col"></param>
/// <returns></returns>
public string GetCellStr(string sheetName, int row, int col)
{
string str = "";
try
{
//Open(xlsFileName);
Excel.Application app = new Excel.Application();
Excel.Workbooks wbs = app.Workbooks;
Excel.Workbook wb = wbs.Add(excelFile); //设置禁止弹出保存和覆盖的询问提示框
app.DisplayAlerts = false;
app.AlertBeforeOverwriting = false;
app.UserControl = true;//如果只想用程序控制该excel而不想让用户操作时候,可以设置为false Excel.Worksheet worksheet = (Excel.Worksheet)wb.Worksheets[sheetName];
Excel.Range r = (Excel.Range)(worksheet.Cells[row, col]);
str = r.Text.ToString();
app.Quit();
//Close();
}
catch (Exception e)
{
MessageBox.Show(e.Message.ToString());
}
return str;
} }
}
这是对excel的查询代码,你看看是否有用
if(){
...
}