@@ -170,13 +170,16 @@ python 版本,请继续使用 0.5.x 。
170
170
171
171
既然有了 pyexcel , 为什么我还要装 django-excel ?
172
172
173
- #. **加快上传文件处理速度 **. **django-excel ** 通过 ExcelMemoryFileUploadHandler 和 TemporaryExcelFileU
174
- ploadHandler 让你直接处理上传的文件。 MemoryFileUploadHandler 把上传的文件存在内存里,这样呢 django-excel
175
- 可以直接从内存读取上传文件。 TemporaryExcelFileUploadHandler 呢,是把上传文件存在临时文件中,django-excel
176
- 则是直接读临时文件。到底是哪个类被 Django 调用呢?是有 FILE_UPLOAD_MAX_MEMORY_SIZE 决定的。如果上传
177
- 文件小于这个最大值,那么 django 会把上传文件存在内存里。否则,就存在临时文件里。
173
+ #. **加快上传文件处理速度 **. **django-excel ** 通过 ExcelMemoryFileUploadHandler 和
174
+ TemporaryExcelFileUploadHandler 让你直接处理上传的文件。
175
+ ExcelemoryFileUploadHandler 把上传的文件存在内存里,这样呢 django-excel 可以直接从内
176
+ 存读取上传文件。 TemporaryExcelFileUploadHandler 呢,是把上传文件存在临时文件中,
177
+ django-excel 则是直接读临时文件。到底是哪个类被 Django 调用呢?
178
+ 是由 FILE_UPLOAD_MAX_MEMORY_SIZE 决定的。如果上传文件小于这个最大值,
179
+ 那么 Django 会把上传文件存在内存里。否则,就存在临时文件里。
178
180
179
- #. **直接把上传数据存入数据库 **. **django-excel ** 用批量方式( bulk_insert )把你的数据导入你的 django model.
181
+ #. **直接把上传数据存入数据库 **. **django-excel ** 用批量方式( bulk_insert )
182
+ 把你的数据导入你的 Django model.
180
183
181
184
182
185
安装
@@ -202,8 +205,8 @@ python 版本,请继续使用 0.5.x 。
202
205
203
206
$ pip install pyexcel-xlsx
204
207
205
- 一反 Django 开箱即用的理念,django-excel 需要开发人员自己选择所需 pyexcel 的套件。主要原因是,
206
- 第三方软件 xlwt, openpyxl, odfpy 也是会拉长下载速度和占用磁盘空间。
208
+ 一反 Django 开箱即用的理念,django-excel 需要开发人员自己选择所需 pyexcel 的套件。
209
+ 主要原因是, 第三方软件 xlwt, openpyxl, odfpy 也是会拉长下载速度和占用磁盘空间。
207
210
208
211
配置
209
212
------------------------
@@ -245,7 +248,8 @@ You will need to update your *settings.py*:
245
248
246
249
git clone https://github.com/pyexcel/django-excel.git
247
250
248
- 整个测试项目是按照 Django 的指南的第 `一 <https://docs.djangoproject.com/en/1.11/intro/tutorial01/ >`_,
251
+ 整个测试项目是按照 Django 的指南的第
252
+ `一 <https://docs.djangoproject.com/en/1.11/intro/tutorial01/ >`_,
249
253
`二 <https://docs.djangoproject.com/en/1.11/intro/tutorial02/ >`_,
250
254
`三 <https://docs.djangoproject.com/en/1.11/intro/tutorial03/ >`_ 部分写的。
251
255
所以,作者就不最述了。如果大家想自己从零开始呢,请移步到 Django 指南,做完第三部分,再回来。
@@ -297,7 +301,7 @@ http://localhost:8000/polls/, 你应该可以看到下面这个上传表格:
297
301
:lines: 14-36
298
302
299
303
300
- **UploadFileForm ** 是 django 的一个文件上传模块。然后往下看 **filehandle **.
304
+ **UploadFileForm ** 是 Django 的一个文件上传模块。然后往下看 **filehandle **.
301
305
它可能是 ExcelInMemoryUploadedFile
302
306
或是 TemporaryUploadedExcelFile。它们两个都继承了 ExcelMixin, 所以它们都又有
303
307
以下的函数,比如 get_sheet, get_array 。
@@ -313,8 +317,8 @@ http://localhost:8000/polls/, 你应该可以看到下面这个上传表格:
313
317
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
314
318
315
319
这个例子展示如何把上传的 excel 的内容直接输入数据库。
316
- `sample-data.xls <https://github.com/pyexcel/django-excel/blob/master/sample-data.xls >`_ 是我们
317
- 样上传的文件 。
320
+ `sample-data.xls <https://github.com/pyexcel/django-excel/blob/master/sample-data.xls >`_
321
+ 是我们要上传的文件 。
318
322
319
323
.. pyexcel-table :: ../../sample-data.xls
320
324
@@ -358,14 +362,15 @@ http://localhost:8000/polls/, 你应该可以看到下面这个上传表格:
358
362
359
363
你可以用管理员界面不数据都删了,再重复上传一次。
360
364
361
- 现在,我们来读一下源代码 `polls/views.py <https://github.com/pyexcel/django-excel/blob/master/polls/views.py#L79 >`_
365
+ 现在,我们来读一下源代码
366
+ `polls/views.py <https://github.com/pyexcel/django-excel/blob/master/polls/views.py#L79 >`_
362
367
请关注这部分代码:
363
368
364
369
.. literalinclude :: ../../polls/views.py
365
370
:lines: 72-92
366
371
367
372
把上传的 excel 存入数据库的功臣是 :meth: `~django_excel.save_book_to_database `.
368
- 函数变量 **models ** 是 django model 数组; **initializers ** 是与之对应的初始化函数。
373
+ 函数变量 **models ** 是 Django model 数组; **initializers ** 是与之对应的初始化函数。
369
374
在代码中,你会注意到,作者没有给 Question 写初始化函数,所以就给了 None;但是把 `choice_func `
370
375
给了 Choice。**mapdicts ** 是一个用来控制数列栏名字的数组。 它的成员可以是一个数组,也
371
376
可以是一个字典::
@@ -404,24 +409,26 @@ http://localhost:8000/polls/export/book,这次呢,一个下载对话框直
404
409
:meth: `~django_excel.make_response_from_tables ` 其实做了所有的事情: 读取数据,
405
410
把它们转换成 xls,再发给浏览器。做为开发人员,你需要给出要输出的表和 excel 文件类型。
406
411
407
- 与此同时,你还可打开另外一个链接: http://localhost:8000/polls/export/sheet 。它会把 ** Question **
408
- 输出成一个单页的表格文件。
412
+ 与此同时,你还可打开另外一个链接: http://localhost:8000/polls/export/sheet 。
413
+ 它会把 ** Question ** 输出成一个单页的表格文件。
409
414
410
415
411
416
直接把 excel 文件渲染成 excel 的样子
412
417
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
413
418
414
- 最开始已经出现了这个渲染的形式。首先呢,`handsontable 开发人员 <https://handsontable.com/ >`_ 做了
419
+ 最开始已经出现了这个渲染的形式。首先呢,
420
+ `handsontable 开发人员 <https://handsontable.com/ >`_ 做了
415
421
所有的工作。其次 `pyexcel-handsontable `_ 仅仅做了集成而已。 想要用这个的话,你需要自己装::
416
422
417
423
$ pip install pyexcel-handsontable
418
424
419
- 现在,我们看看这个是如何调用的。简单地说,就是把输出的文件后最写成: 'handsontable.html' 就可以了。
425
+ 现在,我们看看这个是如何调用的。简单地说,就是把输出的文件后最写成: 'handsontable.html'
426
+ 就可以了。
420
427
421
428
.. literalinclude :: ../../polls/views.py
422
429
:lines: 153-155
423
430
424
- 你可以把 handontable 文件嵌入 django 的模版里面。下面是所需的代码:
431
+ 你可以把 handontable 文件嵌入 Django 的模版里面。下面是所需的代码:
425
432
426
433
.. literalinclude :: ../../polls/views.py
427
434
:lines: 158-189
@@ -439,16 +446,17 @@ and http://localhost:8000/polls/embedded_handson_view_single/.
439
446
440
447
前面讲了如何把一个多表格的 excel 文件里的数据输入数据库。 现在我们看看输入一个表格。
441
448
打开这个链接:http://localhost:8000/polls/imports_sheet/, w
442
- 这次上传 `sample-sheet.xls <https://github.com/pyexcel/django-excel/blob/master/sample-sheet.xls >`_
443
- 然后 django 管理员界面可以查看是否有收到数据。
449
+ 这次上传
450
+ `sample-sheet.xls <https://github.com/pyexcel/django-excel/blob/master/sample-sheet.xls >`_
451
+ 然后 Django 管理员界面可以查看是否有收到数据。
444
452
445
453
下面我们来读代码:
446
454
447
455
.. literalinclude :: ../../polls/views.py
448
456
:lines: 104-116
449
457
450
458
因为是单个表格,所以我们给一个 mapdict 参数并调用
451
- :meth: `~django_excel.ExcelMixin.save_to_database ` 来存到一个 django 模型里。
459
+ :meth: `~django_excel.ExcelMixin.save_to_database ` 来存到一个 Django 模型里。
452
460
453
461
看到了多了一个参数 'name_columns_by_row' 吗?为什么需要它?
454
462
是这样的,一般来讲,如果你的表格的第一行是栏目名字呢,你就不需要它。
@@ -474,15 +482,14 @@ and http://localhost:8000/polls/embedded_handson_view_single/.
474
482
`这部分 pyexcel-io 的代码 <https://github.com/pyexcel/pyexcel-io/blob/master/pyexcel_io/djangobook.py#L98 >`_,
475
483
和 `django-excel 问题 2 <https://github.com/pyexcel/django-excel/issues/2 >`_
476
484
477
- 为了除去这个警告呢,你需要在 django 的管理员界面清空所有数据。然后再试一下。
485
+ 为了除去这个警告呢,你需要在 Django 的管理员界面清空所有数据。然后再试一下。
478
486
479
487
480
488
如果 excel 数据里有一些与数据库数据重叠了,怎么办?
481
489
********************************************************************************
482
490
483
491
你可以提供自己的一个初始化函数。这个数在遇到重叠的数据的时候,返回 None 这样 django-excel
484
- 就会跳过当前的一行数据。
485
- 当然,在初始化函数里,你也可以更新数据库。最重要的是,只有你的初始化函数返回 None,
492
+ 就会跳过当前的一行数据。当然,在初始化函数里,你也可以更新数据库。最重要的是,只有你的初始化函数返回 None,
486
493
django-excel 会尽量用批量输入,而不是一个一个输入数据库。
487
494
488
495
@@ -503,7 +510,7 @@ django-excel 会尽量用批量输入,而不是一个一个输入数据库。
503
510
渲染你的数据
504
511
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
505
512
506
- 为了渲染数据,我们先去 django 的管理员界面,增加一些投票。
513
+ 为了渲染数据,我们先去 Django 的管理员界面,增加一些投票。
507
514
508
515
.. image :: admin-vote.png
509
516
@@ -572,47 +579,47 @@ a little bit more time in coding, documentation and writing interesting posts.
572
579
函数参考
573
580
---------------
574
581
575
- **django-excel ** attaches **pyexcel ** functions to **InMemoryUploadedFile ** and **TemporaryUploadedFile **. Hence, the following functions are available for the uploaded files, e.g. request.FILES['your_uploaded_file'].
582
+ **django-excel ** 把 **pyexcel ** 的函数嫁接到了 **InMemoryUploadedFile ** 和 **TemporaryUploadedFile **。所以,以下的函数
583
+ 都会出现在上载文件的实例里: request.FILES['your_uploaded_file'] 。
576
584
577
585
.. module :: django_excel.ExcelMixin
578
586
579
587
.. method :: get_sheet(sheet_name=None, **keywords)
580
588
581
- :param sheet_name: For an excel book, there could be multiple sheets. If it is left
582
- unspecified, the sheet at index 0 is loaded. For 'csv', 'tsv' file,
583
- *sheet_name * should be None anyway.
584
- :param keywords: additional keywords to :meth: `pyexcel.get_sheet `
585
- :returns: A sheet object
589
+ :param sheet_name: 对于多个表单的 excel 文件,它可以用来指定从哪一个表单取数据。缺省值是第一个表单。
590
+ 要是 csv , tsv 文件的话,可以忽略 *sheet_name * 。
591
+ :param keywords: 其他 :meth: `pyexcel.get_sheet ` 的参数
592
+ :returns: :class: `pyexcel.Sheet `
586
593
587
594
.. method :: get_array(sheet_name=None, **keywords)
588
595
589
- :param sheet_name: same as :meth: `~django_excel.ExcelMixin.get_sheet `
590
- :param keywords: additional keywords to pyexcel library
591
- :returns: a two dimensional array, a list of lists
596
+ :param sheet_name: 和前面 :meth: `~django_excel.ExcelMixin.get_sheet ` 一样。
597
+ :param keywords: 其他 :meth: ` pyexcel.get_array ` 的参数
598
+ :returns: 二维数组(a list of lists)
592
599
593
600
.. method :: iget_array(sheet_name=None, **keywords)
594
601
595
- :param sheet_name: same as :meth: `~django_excel.ExcelMixin.get_sheet `
596
- :param keywords: additional keywords to pyexcel library
597
- :returns: a generator for a two dimensional array, a list of lists
602
+ :param sheet_name: 和前面 :meth: `~django_excel.ExcelMixin.get_sheet ` 一样。
603
+ :param keywords: 其他 :meth: ` pyexcel.iget_array ` 的参数
604
+ :returns: 数组产生器
598
605
599
606
.. method :: get_dict(sheet_name=None, name_columns_by_row=0, **keywords)
600
607
601
- :param sheet_name: same as :meth: `~django_excel.ExcelMixin.get_sheet `
608
+ :param sheet_name: 和前面 :meth: `~django_excel.ExcelMixin.get_sheet ` 一样。
602
609
:param name_columns_by_row: uses the first row of the sheet to be column headers by default.
603
610
:param keywords: additional keywords to pyexcel library
604
611
:returns: a dictionary of the file content
605
612
606
613
.. method :: get_records(sheet_name=None, name_columns_by_row=0, **keywords)
607
614
608
- :param sheet_name: same as :meth: `~django_excel.ExcelMixin.get_sheet `
615
+ :param sheet_name: 和前面 :meth: `~django_excel.ExcelMixin.get_sheet ` 一样。
609
616
:param name_columns_by_row: uses the first row of the sheet to be record field names by default.
610
617
:param keywords: additional keywords to pyexcel library
611
618
:returns: a list of dictionary of the file content
612
619
613
620
.. method :: iget_records(sheet_name=None, name_columns_by_row=0, **keywords)
614
621
615
- :param sheet_name: same as :meth: `~django_excel.ExcelMixin.get_sheet `
622
+ :param sheet_name: 和前面 :meth: `~django_excel.ExcelMixin.get_sheet ` 一样。
616
623
:param name_columns_by_row: uses the first row of the sheet to be record field names by default.
617
624
:param keywords: additional keywords to pyexcel library
618
625
:returns: a generator for a list of dictionary of the file content
0 commit comments