原創(chuàng)|使用教程|編輯:龔雪|2014-02-13 09:17:06.000|閱讀 487 次
概述:本文主要講述如何使用Python REST或者Aspose Cloud SDK for Python來創(chuàng)建并填寫Excel工作簿。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
電子表格廣泛應(yīng)用于存儲、操作、組織和呈現(xiàn)數(shù)據(jù)。一個電子表格可以在單元格系統(tǒng)中存儲大小各異的數(shù)據(jù)集,并將其有序組織于行和列中。一個單元格可以包含一個數(shù)據(jù)值、或一個計算結(jié)果諸如總數(shù)或百分比。這意味著一個電子表格可以HOLD住各種不同類型的數(shù)據(jù)值,以及這些值上的各類計算。
Aspose.Cells for Cloud 讓你能夠創(chuàng)建工作簿、添加工作表、行和列并以任意語言(.NET、Java、PHP、Ruby、Rails、Python、jQuery等等)操作或呈現(xiàn)數(shù)據(jù)。你可以通過支持REST的任意語言或平臺來使用它(幾乎所有平臺和語言都支持REST并提供原生REST客戶端來處理REST APIs)。
你可以使用Python REST或者Aspose Cloud SDK for Python來創(chuàng)建并填寫Excel工作簿。REST和SDK實例均使用Pycurl庫來發(fā)送HTTP請求,處理HTTP響應(yīng),因此你需要安裝Pycurl來使用這些實例。
使用Python REST
你可以用如下URI來在Aspose for Cloud或任意支持的第三方存儲上創(chuàng)建一個默認(rèn)的空的工作簿。
//api.aspose.com/v1.1/cells/NewWorkbook.xlsx
你可以使用如下選擇參數(shù),所有的或指定的參數(shù)都可以依據(jù)你的需求來使用。
在構(gòu)建URI之后,你需要完成如下步驟:
from aspose.cloud.common import * import json try: ####### Create new workbook ####### ####### Section 1 ###### #build URI to create empty workbook str_uri = ‘//api.aspose.com/v1.1/cells/NewWorkbook.xlsx’ ####### End Section 1 ###### ####### Section 2 ###### # sepcify App SID AsposeApp.app_sid = ’77******-1***-4***-a***-80**********’ # sepcify App Key AsposeApp.app_key = ‘*******************************’ #sign URI signed_uri = Utils.sign(Utils(), str_uri) ####### End Section 2 ###### ####### Section 3 ###### Utils.process_command(Utils(), signed_uri, ‘PUT’, ”, ”) ####### End Section 3 ######
使用Python SDK
如果你想要使用Python SDK來創(chuàng)建新的工作簿并添加工作表,你可以從下載該SDK。要使用Python SDK,你需要執(zhí)行如下步驟:
設(shè)置基本產(chǎn)品URI,App SID 和App Key。
創(chuàng)建工作簿類對象并調(diào)用create_empty_workbook()。
執(zhí)行如下代碼來創(chuàng)建空的工作簿:
from aspose.cloud.common import * from aspose.cloud.storage import * from aspose.cloud.cells import * import json try: # sepcify App SID AsposeApp.app_sid = '77******-1***-4***-a***-80**********' # sepcify App Key AsposeApp.app_key = '*******************************' # set base product uri Product.base_product_uri = '//api.aspose.com/v1.1' workbook = Workbook('NewWorkbook.xlsx') print workbook.create_empty_workbook()
如下是REST和SDK實例。
使用Python REST
你可以在Aspose for Cloud 或任意至此后的第三方存儲上使用如下URI來添加一個新的工作表到先前創(chuàng)建的工作簿。
//api.aspose.com/v1.1/cells/NewWorkbook.xlsx /worksheets/NewWorksheet
你可以使用上述URI中的如下可選參數(shù)。所有或指定的參數(shù)都可以按照你的需求來使用。
構(gòu)建URI之后,你需要完成如下步驟:
以下是用來添加新工作表的代碼:
####### Add new worksheet ####### ####### Section 4 ###### #build URI to add a new worksheet str_uri = '//api.aspose.com/v1.1/cells/NewWorkbook.xlsx/worksheets/NewWorksheet' ####### End Section 4 ###### ####### Section 5 ###### #sign URI signed_uri = Utils.sign(Utils(), str_uri) ####### End Section 5 ###### ####### Section 6 ###### Utils.process_command(Utils(), signed_uri, 'PUT', '', '') ####### End Section 6 ######
使用Python SDK
如果你想使用Python SDK,你可以從Aspose Cloud SDK for Python下載該SDK并調(diào)用add_worksheet()方式:
print workbook.add_worksheet('NewWorksheet')
以下是REST和SDK實例。
你可以在Aspose for Cloud 或任意支持的第三方存儲上使用如下URI來設(shè)置單元格的值到之前創(chuàng)建的工作簿。
//api.aspose.com/v1.1/cells/NewWorkbook.xlsx /worksheets/NewWorksheet /cells/B1?value=This is cell B1&type=string
你可以使用上述URI中的如下可選參數(shù)。所有或指定的參數(shù)都可以按照你的需求來使用。
構(gòu)建URI之后,你需要完成如下步驟:
以下是用來設(shè)置單元格值的代碼:
####### Set value of cells ####### ####### Section 7 ###### #build URI to set value of a cell str_uri = '//api.aspose.com/v1.1/cells/NewWorkbook.xlsx/worksheets/NewWorksheet/cells/B1?value=This is cell B1&type=string' ####### End Section 7 ###### ####### Section 8 ###### #sign URI signed_uri = Utils.sign(Utils(), str_uri) ####### End Section 8 ###### ####### Section 9 ###### Utils.process_command(Utils(), signed_uri, 'POST', '', '') ####### End Section 9 ###### ####### Section 10 ###### #build URI to set value of a cell str_uri = '//api.aspose.com/v1.1/cells/NewWorkbook.xlsx/worksheets/NewWorksheet/cells/B2?value=This is cell B2&type=string' #sign URI signed_uri = Utils.sign(Utils(), str_uri) Utils.process_command(Utils(), signed_uri, 'POST', '', '') ####### End Section 10 ######
使用Python SDK
如果你想使用Python SDK,你可以從Aspose Cloud SDK for Python下載該SDK并調(diào)用add_worksheet()方式:
worksheet = Worksheet('NewWorkbook.xlsx', 'NewWorksheet') print worksheet.set_cell_value('B1', 'string', 'This is cell B1') print worksheet.set_cell_value('B2', 'string', 'This is cell B2')
在創(chuàng)建并填寫新的工作簿后,你可以通過下載它。
以下是整合上述步驟的完整REST代碼。
from aspose.cloud.common import * import json try: ####### Create new workbook ####### ####### Section 1 ###### #build URI to create empty workbook str_uri = '//api.aspose.com/v1.1/cells/NewWorkbook.xlsx' ####### End Section 1 ###### ####### Section 2 ###### # sepcify App SID AsposeApp.app_sid = '77******-1***-4***-a***-80**********' # sepcify App Key AsposeApp.app_key = '*******************************' #sign URI signed_uri = Utils.sign(Utils(), str_uri) ####### End Section 2 ###### ####### Section 3 ###### Utils.process_command(Utils(), signed_uri, 'PUT', '', '') ####### End Section 3 ###### ####### Add new worksheet ####### ####### Section 4 ###### #build URI to add a new worksheet str_uri = '//api.aspose.com/v1.1/cells/NewWorkbook.xlsx/worksheets/NewWorksheet' ####### End Section 4 ###### ####### Section 5 ###### #sign URI signed_uri = Utils.sign(Utils(), str_uri) ####### End Section 5 ###### ####### Section 6 ###### Utils.process_command(Utils(), signed_uri, 'PUT', '', '') ####### End Section 6 ###### ####### Set value of cells ####### ####### Section 7 ###### #build URI to set value of a cell str_uri = '//api.aspose.com/v1.1/cells/NewWorkbook.xlsx/worksheets/NewWorksheet/cells/B1?value=This is cell B1&type=string' ####### End Section 7 ###### ####### Section 8 ###### #sign URI signed_uri = Utils.sign(Utils(), str_uri) ####### End Section 8 ###### ####### Section 9 ###### Utils.process_command(Utils(), signed_uri, 'POST', '', '') ####### End Section 9 ###### ####### Section 10 ###### #build URI to set value of a cell str_uri = '//api.aspose.com/v1.1/cells/NewWorkbook.xlsx/worksheets/NewWorksheet/cells/B2?value=This is cell B2&type=string' #sign URI signed_uri = Utils.sign(Utils(), str_uri) Utils.process_command(Utils(), signed_uri, 'POST', '', '') ####### End Section 10 ###### ####### Section 11 ###### #build URI to download output file str_uri = '//api.aspose.com/v1.1/storage/file/NewWorkbook.xlsx' #sign URI signed_uri = Utils.sign(Utils(), str_uri) response_stream = Utils.process_command(Utils(),signed_uri, 'GET') Utils.save_file(Utils(),response_stream, 'NewWorkbook.xlsx') ####### End Section 11 ###### print 'Done' except Exception, ex: print type(ex) # the exception instance print ex.args # arguments stored in .args print ex.errno print ex.strerror finally: raw_input("Press any Key")
以下是整合上述步驟的完整SDK代碼:
from aspose.cloud.common import * from aspose.cloud.storage import * from aspose.cloud.cells import * import json try: # sepcify App SID AsposeApp.app_sid = '77******-1***-4***-a***-80**********' # sepcify App Key AsposeApp.app_key = '*******************************' # set base product uri Product.base_product_uri = '//api.aspose.com/v1.1' workbook = Workbook('NewWorkbook.xlsx') print workbook.create_empty_workbook() print workbook.add_worksheet('NewWorksheet') worksheet = Worksheet('NewWorkbook.xlsx', 'NewWorksheet') print worksheet.set_cell_value('B1', 'string', 'This is cell B1') print worksheet.set_cell_value('B2', 'string', 'This is cell B2') # initialize Folder object folder = Folder() # download output file response_stream = folder.get_file("NewWorkbook.xlsx") # save the stream as local file Utils.save_file(Utils(), response_stream, "NewWorkbook.xlsx") print 'Done' except Exception, ex: print type(ex) # the exception instance print ex.args # arguments stored in .args print ex.errno print ex.strerror finally: raw_input("Press any Key")
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都控件網(wǎng)