我编写了一个可以使用Microsoft Data Access Engine读取excel 2007文件的代码,下面的代码片段适用于大多数文件,但它恰好适用于大多数excel文件,即 .xlsx,< /em> .xls但是当它在objConn.Open()时失败;对于格式有问题的excel文件,请参考下面的图片
无法打开ole Db Connection说明错误外部表格未达到预期格式。此导入过程的另一个问题是
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM "+ SelectionSheet, objConn);
无法阅读以空格开头的工作表,我们将非常感谢您解决此问题。
public DataTable ReadExcel(string Path, ArrayList IgnoreString, ArrayList IgnoreColumn)
{
DataTable dtReturn = new DataTable();
DataTable dtPrintable = new DataTable();
DataTable dtTemp = new DataTable();
try
{
string sConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=" + Path + ";" + "Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1;\"";
OleDbConnection objConn = new OleDbConnection(sConnectionString);
objConn.Open();
DataTable dtSheetnames = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
DataTable dtTesting = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.DbInfoLiterals, new object[] {});
DataTable dtTesting2 = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables_Info, new object[] { });
string SelectionSheet = dtSheetnames.Rows[0][2].ToString();
if (SelectionSheet.Contains("'") )
{
SelectionSheet = SelectionSheet.Remove(0, 1);
SelectionSheet = "[" + SelectionSheet;
SelectionSheet = SelectionSheet.Remove(SelectionSheet.Length - 1, 1);
// -- Mod by zeemz on 23 dec
// string PrintArea = SelectionSheet + "Print_Area]";
SelectionSheet = SelectionSheet + "]";
}
else
{
SelectionSheet = "["+ SelectionSheet + "]";
}
//OleDbCommandBuilder objCmdBuilder = new OleDbCommandBuilder(
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM "+ SelectionSheet, objConn);
OleDbDataAdapter objAdapter = new OleDbDataAdapter();
DataSet objDataSet = new DataSet();
DataSet PrintAreads = new DataSet();
objAdapter.SelectCommand = objCmdSelect;
objAdapter.Fill(objDataSet);
// -- Mod by zeemz on 23 dec
//objCmdSelect.CommandText = "SELECT * FROM " + PrintArea;
//objAdapter.Fill(PrintAreads);
objConn.Close();
dtReturn = objDataSet.Tables[0].Copy();
// dtPrintable = PrintAreads.Tables[0].Copy();
// -- Mod by zeemz on 23 dec
//if (dtPrintable.Columns.Count != dtReturn.Columns.Count)
//{
// int TotalPrintable = dtPrintable.Columns.Count;
// int TotalComing = dtReturn.Columns.Count;
// int StartRemovingPos = TotalComing - TotalPrintable;
// for (int i = TotalPrintable; dtPrintable.Columns.Count != dtReturn.Columns.Count; i++)
// {
// dtReturn.Columns.RemoveAt(i);
// i = i - 1 ;
// }
//}
int iCount = 0;
while (iCount <= dtReturn.Rows.Count - 1)
{
if (isRowEmpty(dtReturn.Rows[iCount]))
{
dtReturn.Rows.RemoveAt(iCount);
}
else
{
iCount += 1;
}
}
//now applying the filters
//column ignore
for (int i = IgnoreColumn.Count - 1; i >= 0; i--)
{
dtReturn.Columns.RemoveAt((int)IgnoreColumn[i]);
}
//string ignore
for (int i = IgnoreString.Count - 1; i >= 0; i--)
{
for (int j = dtReturn.Rows.Count - 1; j >= 0; i--)
{
foreach (DataColumn dCol in dtReturn.Columns)
{
if (dtReturn.Rows[j][dCol.ColumnName].ToString().ToLower().Contains(IgnoreString[i].ToString().ToLower()))
{
dtReturn.Rows.RemoveAt(j);
break;
}
}
}
}
/* Hack to get rid of DateTime Columns */
// added by zeemz
dtTemp = dtReturn.Clone();
dtTemp.Clear();
foreach (DataColumn tempColumn in dtTemp.Columns)
{
// if (tempColumn.DataType == typeof(DateTime))
// {
tempColumn.DataType = typeof(String);
// }
}
foreach (DataRow tempRow in dtReturn.Rows)
{
DataRow insRow = dtTemp.NewRow();
foreach (DataColumn tempColumn in dtReturn.Columns)
{
if (tempColumn.DataType == typeof(DateTime))
{
if (!String.IsNullOrEmpty(tempRow[tempColumn.ColumnName.ToString()].ToString()))
{
insRow[tempColumn.ColumnName.ToString()] = Convert.ToDateTime(tempRow[tempColumn.ColumnName.ToString()].ToString()).ToString("yyyyMMddhhmmss");
}
else
{
insRow[tempColumn.ColumnName.ToString()] = "";
}
}
else
{
insRow[tempColumn.ColumnName.ToString()] = tempRow[tempColumn.ColumnName.ToString()].ToString();
}
}
dtTemp.Rows.Add(insRow);
}
}
catch (Exception ex)
{
throw ex;
}
return dtTemp;
}
答案 0 :(得分:2)
我碰巧发现当您修改xlsx时手动或编程修改xlsx文件时不存在excel,格式不能保持原样并且由于OleDbDataAdapter无法处理修改后出现上述错误文件和Excel它自己可以修复损坏的文件显示文件按预期。