Forum Discussion

jitterbitse's avatar
jitterbitse
Copper Contributor
May 19, 2023

This workbook couldn't be opened because the file format may not be matching with the file extension

Through the code attached below, I'm trying to create a spreadsheet. The files get created but I'm not able to preview it. Whereas, when I set content-type as application/text-plain to generate a text file. Not only does the file gets created but I'm also able to preview it.

String url = "https://myTenant.sharepoint.com/_api/web/GetFolderByServerRelativeUrl('/Shared%20Documents')/Files/add(url='myFile.xlsx',overwrite=true)";
List<NameValuePair> nameValueHeaders = null;
JSONObject requestBody = new JSONObject();
String fileContent = "dummy";
byte[] contentBytes = fileContent.getBytes(StandardCharsets.UTF_8);
HttpEntity entity = new ByteArrayEntity(contentBytes, ContentType.create("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"));// StringEntity(requestBody.toString(), ContentType.APPLICATION_JSON);
HttpUriRequestBase requestBase = httpHandler.getHttpPost(url, nameValueHeaders, entity);
requestBase.setHeader("Authorization", "Bearer " + token);
requestBase.setHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON);
HttpCallResponse callresponse = httpHandler.executeHttpRequest(requestBase);
System.out.println(callresponse.getResponse());

 

 

Resources