以下是我的代码:
#!/usr/bin/env python
# -*- coding: cp936 -*-
from pyExcelerator import *
import pymssql
con = pymssql.connect(host='10.0.4.11',user='sa',password='sa',database='backdata')
cur = con.cursor()
w = Workbook()
ws = w.add_sheet('F')
i=0
j=0
sql="select * from table1"
cur.execute(sql)
f=cur.fetchall()
#while i<cur.rowcount:
while j<9:
k=f[i][j]
print k
ws.write(i,j,k,style0)
j=j+1
# i=i+1
w.save('table.xls')
7 个解决方案
#1
再问一下,ws = w.add_sheet('F') 表示添加一个EXCEL工作表,如果只是修改已经存在的EXCEL文件的工作表,应该用什么代码呢?
#2
先蒙蒙看吧:
把k替换成:
或者:
多试试吧。我这边是没环境。
另外,你是在Windows下还是Linux?系统的编码是Cp936还是UTF-8?
ws.write(i,j,k,style0)
把k替换成:
k.decode('cp936')
或者:
k.decode('cp936').encode('utf-8')
多试试吧。我这边是没环境。
另外,你是在Windows下还是Linux?系统的编码是Cp936还是UTF-8?
#3
pyExcelerator是自己创建一个内部的结构模拟一个Excel表,最后保存的时候才以二进制格式写成一个.xls文件,所以不能用来修改已存在的.xls文件。
如果想修改已存在的.xls文件,一种方式是先全部读出来,再改写你想改写的部分,最后保存成一个新的文件。另一种方式是采用OLE的操作方式。我下了个代码,封装了win32com.client里的一些方法。不过我没有试过。我一般只关心数据,不管格式,所以只使用第一方式,直接利用pyExcelerator的parse_xls函数读出全部内容,自己封装了些方法,改写后保存为新文件。
我把那个使用win32com.client的封装贴上来吧。我没有试过,所以有问题也不要问我T_T.
quickexcel.py
from win32com.client import constants, Dispatch
import pythoncom
import os
borderTop = 3
borderBottom = 4
borderLeft = 1
borderRight = 2
borderSolid = 1
borderDashed = 2
borderDotted = 3
colorBlack = 1
directionUp = -4162
directionDown = -4121
directionLeft = -4131
directionRight = -4152
class ExcelDocument(object):
"""
Some convenience methods for Excel documents accessed
through COM.
"""
def __init__(self, visible=False):
self.app = Dispatch("Excel.Application")
self.app.Visible = visible
self.sheet = 1
def new(self, filename=None):
"""
Create a new Excel workbook. If 'filename' specified,
use the file as a template.
"""
self.app.Workbooks.Add(filename)
def open(self, filename):
"""
Open an existing Excel workbook for editing.
"""
self.app.Workbooks.Open(filename)
def set_sheet(self, sheet):
"""
Set the active worksheet.
"""
self.sheet = sheet
def get_range(self, range):
"""
Get a range object for the specified range or single cell.
"""
return self.app.ActiveWorkbook.Sheets(self.sheet).Range(range)
def get_cell(self, r,c):
return self.app.ActiveWorkbook.Sheets(self.sheet).Cells(r,c).Value
def set_value(self, cell, value=''):
"""
Set the value of 'cell' to 'value'.
"""
self.get_range(cell).Value = value
def get_value(self, cell):
"""
Get the value of 'cell'.
"""
value = self.get_range(cell).Value
if isinstance(value, tuple):
value = [v[0] for v in value]
return value
def set_border(self, range, side, line_style=borderSolid, color=colorBlack):
"""
Set a border on the specified range of cells or single cell.
'range' = range of cells or single cell
'side' = one of borderTop, borderBottom, borderLeft, borderRight
'line_style' = one of borderSolid, borderDashed, borderDotted, others?
'color' = one of colorBlack, others?
"""
range = self.get_range(range).Borders(side)
range.LineStyle = line_style
range.Color = color
def sort(self, range, key_cell):
"""
Sort the specified 'range' of the activeworksheet by the
specified 'key_cell'.
"""
range.Sort(Key1=self.get_range(key_cell), Order1=1, Header=0, OrderCustom=1, MatchCase=False, Orientation=1)
def hide_row(self, row, hide=True):
"""
Hide the specified 'row'.
Specify hide=False to show the row.
"""
self.get_range('a%s' % row).EntireRow.Hidden = hide
def hide_column(self, column, hide=True):
"""
Hide the specified 'column'.
Specify hide=False to show the column.
"""
self.get_range('%s1' % column).EntireColumn.Hidden = hide
def delete_row(self, row, shift=directionUp):
"""
Delete the entire 'row'.
"""
self.get_range('a%s' % row).EntireRow.Delete(Shift=shift)
def delete_column(self, column, shift=directionLeft):
"""
Delete the entire 'column'.
"""
self.get_range('%s1' % column).EntireColumn.Delete(Shift=shift)
def fit_column(self, column):
"""
Resize the specified 'column' to fit all its contents.
"""
self.get_range('%s1' % column).EntireColumn.AutoFit()
def set_cell_red(self, r,c):
self.app.ActiveWorkbook.Sheets(self.sheet).Cells(r,c).Font.ColorIndex = 3
def save(self):
"""
Save the active workbook.
"""
self.app.ActiveWorkbook.Save()
def save_as(self, filename, delete_existing=False):
"""
Save the active workbook as a different filename.
If 'delete_existing' is specified and the file already
exists, it will be deleted before saving.
"""
if delete_existing and os.path.exists(filename):
os.remove(filename)
self.app.ActiveWorkbook.SaveAs(filename)
def print_out(self):
"""
Print the active workbook.
"""
self.app.Application.PrintOut()
def close(self):
"""
Close the active workbook.
"""
self.app.ActiveWorkbook.Close()
def quit(self):
"""
Quit Excel.
"""
return self.app.Quit()
#4
ws.write(i,j,k,style0)改为ws.write(i,j,k.decode(codetype),style0)
其中codetype为你的mysql数据库的编码方式。
其中codetype为你的mysql数据库的编码方式。
#5
decode函数可不可以
#6
呵呵,先收下3楼的代码了。
可能过几天要用到这方面的例子。
可能过几天要用到这方面的例子。
#7
楼主的问题应是4、5楼兄弟说的内码问题,用decode之类的函数试下。
#1
再问一下,ws = w.add_sheet('F') 表示添加一个EXCEL工作表,如果只是修改已经存在的EXCEL文件的工作表,应该用什么代码呢?
#2
先蒙蒙看吧:
把k替换成:
或者:
多试试吧。我这边是没环境。
另外,你是在Windows下还是Linux?系统的编码是Cp936还是UTF-8?
ws.write(i,j,k,style0)
把k替换成:
k.decode('cp936')
或者:
k.decode('cp936').encode('utf-8')
多试试吧。我这边是没环境。
另外,你是在Windows下还是Linux?系统的编码是Cp936还是UTF-8?
#3
pyExcelerator是自己创建一个内部的结构模拟一个Excel表,最后保存的时候才以二进制格式写成一个.xls文件,所以不能用来修改已存在的.xls文件。
如果想修改已存在的.xls文件,一种方式是先全部读出来,再改写你想改写的部分,最后保存成一个新的文件。另一种方式是采用OLE的操作方式。我下了个代码,封装了win32com.client里的一些方法。不过我没有试过。我一般只关心数据,不管格式,所以只使用第一方式,直接利用pyExcelerator的parse_xls函数读出全部内容,自己封装了些方法,改写后保存为新文件。
我把那个使用win32com.client的封装贴上来吧。我没有试过,所以有问题也不要问我T_T.
quickexcel.py
from win32com.client import constants, Dispatch
import pythoncom
import os
borderTop = 3
borderBottom = 4
borderLeft = 1
borderRight = 2
borderSolid = 1
borderDashed = 2
borderDotted = 3
colorBlack = 1
directionUp = -4162
directionDown = -4121
directionLeft = -4131
directionRight = -4152
class ExcelDocument(object):
"""
Some convenience methods for Excel documents accessed
through COM.
"""
def __init__(self, visible=False):
self.app = Dispatch("Excel.Application")
self.app.Visible = visible
self.sheet = 1
def new(self, filename=None):
"""
Create a new Excel workbook. If 'filename' specified,
use the file as a template.
"""
self.app.Workbooks.Add(filename)
def open(self, filename):
"""
Open an existing Excel workbook for editing.
"""
self.app.Workbooks.Open(filename)
def set_sheet(self, sheet):
"""
Set the active worksheet.
"""
self.sheet = sheet
def get_range(self, range):
"""
Get a range object for the specified range or single cell.
"""
return self.app.ActiveWorkbook.Sheets(self.sheet).Range(range)
def get_cell(self, r,c):
return self.app.ActiveWorkbook.Sheets(self.sheet).Cells(r,c).Value
def set_value(self, cell, value=''):
"""
Set the value of 'cell' to 'value'.
"""
self.get_range(cell).Value = value
def get_value(self, cell):
"""
Get the value of 'cell'.
"""
value = self.get_range(cell).Value
if isinstance(value, tuple):
value = [v[0] for v in value]
return value
def set_border(self, range, side, line_style=borderSolid, color=colorBlack):
"""
Set a border on the specified range of cells or single cell.
'range' = range of cells or single cell
'side' = one of borderTop, borderBottom, borderLeft, borderRight
'line_style' = one of borderSolid, borderDashed, borderDotted, others?
'color' = one of colorBlack, others?
"""
range = self.get_range(range).Borders(side)
range.LineStyle = line_style
range.Color = color
def sort(self, range, key_cell):
"""
Sort the specified 'range' of the activeworksheet by the
specified 'key_cell'.
"""
range.Sort(Key1=self.get_range(key_cell), Order1=1, Header=0, OrderCustom=1, MatchCase=False, Orientation=1)
def hide_row(self, row, hide=True):
"""
Hide the specified 'row'.
Specify hide=False to show the row.
"""
self.get_range('a%s' % row).EntireRow.Hidden = hide
def hide_column(self, column, hide=True):
"""
Hide the specified 'column'.
Specify hide=False to show the column.
"""
self.get_range('%s1' % column).EntireColumn.Hidden = hide
def delete_row(self, row, shift=directionUp):
"""
Delete the entire 'row'.
"""
self.get_range('a%s' % row).EntireRow.Delete(Shift=shift)
def delete_column(self, column, shift=directionLeft):
"""
Delete the entire 'column'.
"""
self.get_range('%s1' % column).EntireColumn.Delete(Shift=shift)
def fit_column(self, column):
"""
Resize the specified 'column' to fit all its contents.
"""
self.get_range('%s1' % column).EntireColumn.AutoFit()
def set_cell_red(self, r,c):
self.app.ActiveWorkbook.Sheets(self.sheet).Cells(r,c).Font.ColorIndex = 3
def save(self):
"""
Save the active workbook.
"""
self.app.ActiveWorkbook.Save()
def save_as(self, filename, delete_existing=False):
"""
Save the active workbook as a different filename.
If 'delete_existing' is specified and the file already
exists, it will be deleted before saving.
"""
if delete_existing and os.path.exists(filename):
os.remove(filename)
self.app.ActiveWorkbook.SaveAs(filename)
def print_out(self):
"""
Print the active workbook.
"""
self.app.Application.PrintOut()
def close(self):
"""
Close the active workbook.
"""
self.app.ActiveWorkbook.Close()
def quit(self):
"""
Quit Excel.
"""
return self.app.Quit()
#4
ws.write(i,j,k,style0)改为ws.write(i,j,k.decode(codetype),style0)
其中codetype为你的mysql数据库的编码方式。
其中codetype为你的mysql数据库的编码方式。
#5
decode函数可不可以
#6
呵呵,先收下3楼的代码了。
可能过几天要用到这方面的例子。
可能过几天要用到这方面的例子。
#7
楼主的问题应是4、5楼兄弟说的内码问题,用decode之类的函数试下。