Office 365 - Create *.docx | *.pptx | *.xlsx files

无人久伴 提交于 2020-01-17 05:18:12

问题


I'm working on a web project which uses the OneDrive API for file management purposes. I wanted to create *.docx | *.pptx | *.xlsx files but unfortunately, none of the OneDrive APIs support creating empty/new files. What I tried was to upload an empty/new file from my server to OneDrive. While the upload works fine, the file does not open in Office Online nor in local Office.

From this blog, I was able to generate a Word file and upload it.

function createNewDoc(name) {
var htmlString = "<html><body></body></html>";
var byteNumbers = new Uint8Array(htmlString.length);

for (var i = 0; i < htmlString.length; i++) {
   byteNumbers[i] = htmlString.charCodeAt(i);
    }
    var blob = new Blob([byteNumbers], {type: "text/html"});
    socialAPI.uploadFile('windows', blob, 'name', 'folder.87ash7ashb7a2').then(function (resp) {
        console.log("response for uploading file", resp);
    })
}

This file worked for Word. However, the method doesn't work for Excel and Powerpoint.

So ultimately, I was not able to do anything using the OneDrive API. I started looking at Office 365 API if there's anything which allows creation of above mentioned files but I couldn't find anything like that. (P.S: I'm looking at REST APIs)

Another thing I noticed is Box is able to do the same and open it in a window, but the url is in the format https://app.box.com/services/box_for_office_online. Does Box have some special collaboration with Microsoft to provide such a service?


回答1:


"Empty" Office files aren't really 0 byte files. The desktop Office applications (except Excel) are able to take a 0 byte file and make it become an Office document (insert the "template" that makes a file an Office file). The online Office apps currently don't support doing that.

As there is no API currently to create a new file of a particular type, you'll need to work around it. To do this, you need to store and upload "template" files for Word, Excel, and PowerPoint. The example you provided is one way of doing this for Word, where you've created an empty HTML page and uploaded it.

For Excel and PowerPoint, you would need to do something similar by creating an empty file and uploading that as the "new" file whenever your app wants to create a new empty file. However, instead of being able to store a simple HTML string as the template file content, you'll need the actual binary content of a new file, which can be up to ~30kb depending on the type.

We are working to improve the Web apps so they can take a 0 byte file and "convert" it into a new document, but that is still a bug today.



来源:https://stackoverflow.com/questions/37153916/office-365-create-docx-pptx-xlsx-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!