使用 C# 读取损坏的 excel
Reading corrupted excel using C#
我想使用 C# 控制台打开损坏的 excel 并从中读取数据 application.I 正在尝试使用以下代码:
public System.Data.DataTable EXc(string path, string savedFile)
{
try
{
Missing missing = Missing.Value;
Excel.Application excel = new Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workbook = excel.Workbooks.Open(path,
missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing,
missing, missing, missing, Microsoft.Office.Interop.Excel.XlCorruptLoad.xlRepairFile);
workbook.Close(true, missing, missing);
if (workbook != null)
{
workbook.SaveAs(savedFile, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault,
missing, missing, missing, missing,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, missing,
missing, missing, missing, missing);
}
但我遇到了异常
specified cast is not valid
排队
workbook.SaveAs(savedFile, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault,
missing, missing, missing, missing,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, missing,
missing, missing, missing, missing);
你能帮我解决这个问题吗?
我认错了:
我试图保存一个关闭的工作簿,可能是因为我得到了那个异常
Specified cast is not valid
使用互操作打开损坏的 excel 后,我们可以使用 oledb 连接并将该数据复制到数据表或数据集中并可以使用。
public System.Data.DataTable CorruptedExcel(字符串路径,字符串保存文件)
{
try
{
Missing missing = Missing.Value;
Excel.Application excel = new Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workbook = excel.Workbooks.Open(path, CorruptLoad: Microsoft.Office.Interop.Excel.XlCorruptLoad.xlRepairFile);
var connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=YES;TypeGuessRows=0;ImportMixedTypes=Text\"";
//var connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;IMEX=1;HDR=NO;TypeGuessRows=0;ImportMixedTypes=Text\""; ;
using (var conn = new OleDbConnection(connectionString))
{
conn.Open();
var sheets = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "SELECT * FROM [" + sheets.Rows[0]["TABLE_NAME"].ToString() + "] ";
var adapter = new OleDbDataAdapter(cmd);
adapter.Fill(dt);
}
}
return dt;
}
catch (Exception e)
{
throw new Exception("ReadingExcel: Excel file could not be read! Check filepath.\n"
+ e.Message + e.StackTrace);
}
}
我想使用 C# 控制台打开损坏的 excel 并从中读取数据 application.I 正在尝试使用以下代码:
public System.Data.DataTable EXc(string path, string savedFile)
{
try
{
Missing missing = Missing.Value;
Excel.Application excel = new Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workbook = excel.Workbooks.Open(path,
missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing,
missing, missing, missing, Microsoft.Office.Interop.Excel.XlCorruptLoad.xlRepairFile);
workbook.Close(true, missing, missing);
if (workbook != null)
{
workbook.SaveAs(savedFile, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault,
missing, missing, missing, missing,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, missing,
missing, missing, missing, missing);
}
但我遇到了异常
specified cast is not valid
排队
workbook.SaveAs(savedFile, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault,
missing, missing, missing, missing,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, missing,
missing, missing, missing, missing);
你能帮我解决这个问题吗?
我认错了:
我试图保存一个关闭的工作簿,可能是因为我得到了那个异常
Specified cast is not valid
使用互操作打开损坏的 excel 后,我们可以使用 oledb 连接并将该数据复制到数据表或数据集中并可以使用。
public System.Data.DataTable CorruptedExcel(字符串路径,字符串保存文件) {
try
{
Missing missing = Missing.Value;
Excel.Application excel = new Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workbook = excel.Workbooks.Open(path, CorruptLoad: Microsoft.Office.Interop.Excel.XlCorruptLoad.xlRepairFile);
var connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=YES;TypeGuessRows=0;ImportMixedTypes=Text\"";
//var connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;IMEX=1;HDR=NO;TypeGuessRows=0;ImportMixedTypes=Text\""; ;
using (var conn = new OleDbConnection(connectionString))
{
conn.Open();
var sheets = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "SELECT * FROM [" + sheets.Rows[0]["TABLE_NAME"].ToString() + "] ";
var adapter = new OleDbDataAdapter(cmd);
adapter.Fill(dt);
}
}
return dt;
}
catch (Exception e)
{
throw new Exception("ReadingExcel: Excel file could not be read! Check filepath.\n"
+ e.Message + e.StackTrace);
}
}