我已经从Office 365共享点下载了Excel,文件下载成功,我已经阅读了这个post。
"我编辑了什么"
逻辑1是I加的
我已经使用httpClient
从Office 365共享点下载Excel,Excel能够下载到给定的路径,但Excel也得到了同样的错误之王,因为我在这篇文章中提到。
private async Task SaveFile(string fileUrl, string pathToSave, string username, object securedPassword)
{
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", ""+username+":"+securedPassword+"".ToString());
var httpResult = await httpClient.GetAsync(fileUrl);
var resultStream = await httpResult.Content.ReadAsStreamAsync();
var filePath = pathToSave;
System.IO.FileStream fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite);
resultStream.CopyTo(fileStream);
}
调用方法为
private void button1_Click(object sender, EventArgs e)
{
const string username = "kumar@xxxxx.com";
const string password = "Welcome@1055";
const string url = "https://xxxxx.sharepoint.com/sites/files/my%20Files/excel%20folder";
const string templocation = @"c:\Downloads\Sharepoint\Task.xlsx";
var securedPassword = new SecureString();
foreach (var c in password.ToCharArray()) securedPassword.AppendChar(c);
var credentials = new SharePointOnlineCredentials(username, securedPassword);
_ = SaveFile(url, templocation, username, securedPassword);
}
逻辑2:
这是excel下载的代码
private void button1_Click(object sender, EventArgs e)
{
const string username = "kumar@xxxxx.com";
const string password = "Welcome@1055";
const string url = "https://xxxxx.sharepoint.com/sites/files/my%20Files/excel%20folder";
const string templocation = @"c:\Downloads\Sharepoint\Task.xlsx";
var securedPassword = new SecureString();
foreach (var c in password.ToCharArray()) securedPassword.AppendChar(c);
var credentials = new SharePointOnlineCredentials(username, securedPassword);
DownloadFile(url, credentials, templocation);
}
private static void DownloadFile(string webUrl, ICredentials credentials, string fileRelativeUrl)
{
using (var client = new WebClient())
{
client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
client.Headers.Add("User-Agent: Other");
client.Credentials = credentials;
client.DownloadFile(webUrl, fileRelativeUrl);
}
}
Excel成功下载后,我尝试从下载的文件夹手动打开Excel,但此时出现此错误
Excel cannot open the file 'Task.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.
示例img:
我还更改了扩展名(xls,csv)进行测试,但结果是相同的(得到相同类型的错误)
我尝试使用逻辑打开Excel文件,也遇到了同样的错误
下面是我的代码:
public DataTable ReadExcel_tskmgmt(string fileName, string fileExt, string sstext)
{
fileExt = fileExt.ToLower();
string conn = string.Empty;
DataTable dtexcel = new DataTable();
string ssssheetname = "-";
try
{
if (fileExt.CompareTo(".csv") == 0)
conn = string.Format(@"Provider=Microsoft.Jet.OleDb.4.0; Data Source={0};Extended Properties=""Text;HDR=YES;FMT=Delimited""", Path.GetDirectoryName(fileName));
else if (fileExt.CompareTo(".xls") == 0)
conn = @"provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";Extended Properties='Excel 8.0;HRD=Yes;IMEX=1';"; //for below excel 2007
else
conn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1';"; //for above excel 2007
using (OleDbConnection con = new OleDbConnection(conn))
{
try
{
object missing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
//Getting error here
//Excel cannot open the file 'TaskMgmt.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.
Microsoft.Office.Interop.Excel.Workbook wb = excel.Workbooks.Open(fileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
//Microsoft.Office.Interop.Excel.Workbook wb = excel.Workbooks.Open(fileName);
foreach (Microsoft.Office.Interop.Excel.Worksheet sheet in wb.Sheets)
{
if (sstext.ToLower() == sheet.Name.ToLower())
{
ssssheetname = (sheet.Name);
break;
}
}
wb.Close(false, missing, missing);
excel.Quit();
string query = "";
if (sstext == "")
query = "select * from [" + ssssheetname + "$]";
if (sstext != "")
query = "select * from [" + ssssheetname + "$]";
if (fileExt.ToLower() == ".csv")
{
if (sstext == "")
query = "select * from [" + Path.GetFileName(fileName) + "]";
}
OleDbDataAdapter oleAdpt = new OleDbDataAdapter(query, con); //here we read data from sheet1
oleAdpt.Fill(dtexcel); //fill excel data into dataTable
}
catch (Exception ee)
{
AppendLog("excel in read inner err:" + ee.ToString());
}
}
}
catch (Exception ee)
{
AppendLog("reading excel err:" + ee.ToString());
}
return dtexcel;
}
为了您的理解:我已经从该路径手动下载了Excel https://xxxxx.sharepoint.com/sites/files/my%20Files/excel%20folder
,成功下载了Excel,并能够手动打开Excel,出现任何类型的错误,通过使用该方法DataTable ReadExcel_tskmgmt
,我能够打开并阅读手动下载的Excel(测试目的,我在代码工作或不工作的情况下进行了此操作)
当我试图从逻辑中下载时,出现了同样的excel。我得到了这种错误Excel cannot open the file 'Task.xlsx' because the file format or file extension is not valid.
为什么这个错误来了?我在哪里做的错误和如何实现这一点?
注:逻辑1和逻辑2均显示相同类型的错误。
给我建议你最好的解决方案。
1条答案
按热度按时间92vpleto1#
有同样的问题,
很奇怪但解决方法是
解决方案是直接下载到您的笔记本电脑/台式机的C:/文件夹。
不得不用Onedrive做一些事情。正在与Onedrive同步的文件夹,我得到了损坏的文件问题