string StrConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + Path + "';Extended Properties='Excel 12.0;HDR=NO; IMEX=1'";
OleDbConnection Conn = new OleDbConnection(StrConn);
Conn.Open();
string StrExcel = "";
OleDbDataAdapter MyCommand = null;
DataSet Ds = null;
StrExcel = "select * from [sheet1$]";
MyCommand = new OleDbDataAdapter(StrExcel, StrConn);
Ds = new DataSet();
MyCommand.Fill(Ds, "[sheet1$]");
Conn.Close();
//定义二维数组存储 Excel 表中读取的数据
DataTable ExcelTable = Ds.Tables["[sheet1$]"];
int Colums = RealColumn(Path);//列数
int Rows = RealRow(Path);//行数
string[][] StoryData = new string[Rows][];
// 定义交叉数组
for (int i = 0; i < Rows; i++)
StoryData[i] = new string[Colums];
// 交叉数组赋值
for (int i = 0; i < Rows; i++)
for (int j = 0; j < Colums; j++)
StoryData[i][j] = ExcelTable.Rows[i][j].ToString().Trim();
return StoryData;
有时候数据读取出来是空值,请问怎么解决?
我读取数据都是正常的,就只有个别几个数据读出来是空值
追答// 交叉数组赋值
for (int i = 0; i < Rows; i++)
for (int j = 0; j < Colums; j++)
StoryData[i][j] = Convert.IsDBNull(ExcelTable.Rows[i][j]) ? "" :ExcelTable.Rows[i][j].ToString().Trim();
return StoryData;
你这行这么改
用Convert.IsDBNull()函数判断表里是否null值,用==null是无法判断datatable里null的