关于headerstyle的信息
本文目录一览:
- 1、Python怎样给Excel 2007版以上的xlsx文件追加数据
- 2、菜鸟提问:如何让GridView的表头内容居中
- 3、Python 操作Excel,怎么插入一行或一列
- 4、用C#,将DataTable中的数据导出到Excel2007,求各位大神指导。。而且一定要有如下图的对话框进行提示。。
Python怎样给Excel 2007版以上的xlsx文件追加数据
背景
Python中,xlrd主要用来读取excel文件, xlwt主要用来写文件,本文主要介绍打开已经存在的excel的xls文件,然后在最后新的一行的数据。要用到xlutils包,它依赖于前两个包。
折腾过程
1.找到了参考资料:
writing to existing workbook using xlwt
其实是没有直接实现:
打开已有的excel文件,然后在文件最后写入,添加新数据
的函数的。
只不过,可以利用:
Working with Excel Files in Python
中的库,组合实现。
2. writing to existing workbook using xlwt
给出了示例代码:
?
12345678910111213141516171819202122
rom xlutils.copy importcopy fromxlrd importopen_workbook fromxlwt importeasyxf START_ROW =297 # 0 based (subtract 1 from excel row number)col_age_november =1col_summer1 =2col_fall1 =3 rb =open_workbook(file_path,formatting_info=True)r_sheet =rb.sheet_by_index(0) # read only copy to introspect the filewb =copy(rb) # a writable copy (I can't read values out of this, only write to it)w_sheet =wb.get_sheet(0) # the sheet to write to within the writable copy forrow_index inrange(START_ROW, r_sheet.nrows): age_nov =r_sheet.cell(row_index, col_age_november).value ifage_nov ==3: #If 3, then Combo I 3-4 year old for both summer1 and fall1 w_sheet.write(row_index, col_summer1, 'Combo I 3-4 year old') w_sheet.write(row_index, col_fall1, 'Combo I 3-4 year old') wb.save(file_path +'.out' + os.path.splitext(file_path)[-1])
3. 刚又看到,有更简洁的代码:
?
1234
fromxlutils.copy importcopyw =copy('book1.xls')w.get_sheet(0).write(0,0,"foo")w.save('book2.xls')
4.现在打算去试试。
先去安装xlrd:
【记录】Python中安装xlrd模块
6.再去安装xlutils:
【记录】Python中安装可以读写excel的xls文件的xlutils模块(需依赖于xlrd和xlwt)
7.接着可以去写代码了。
8.先是:
【已解决】Python中使用xlutils.copy出错:AttributeError: ‘module’ object has no attribute ‘copy’
9.后是:
【已解决】Python中使用xlutils的copy出错:AttributeError: ‘str’ object has no attribute ‘datemode’
10.后来是用如下代码:
?
1234567891011121314151617181920212223242526272829303132
importxlwt;importxlrd;#import xlutils;fromxlutils.copy importcopy; #init xls file#styleBlueBkg= xlwt.easyxf('pattern: pattern solid, fore_colour sky_blue;');#styleBold = xlwt.easyxf('font: bold on');styleBoldRed =xlwt.easyxf('font: color-index red, bold on');headerStyle =styleBoldRed;wb =xlwt.Workbook();ws =wb.add_sheet(gConst['xls']['sheetName']);ws.write(0, 0, "Header", headerStyle);ws.write(0, 1, "CatalogNumber", headerStyle);ws.write(0, 2, "PartNumber", headerStyle);wb.save(gConst['xls']['fileName']); #open existed xls file#newWb = xlutils.copy(gConst['xls']['fileName']);#newWb = copy(gConst['xls']['fileName']);oldWb =xlrd.open_workbook(gConst['xls']['fileName']);printoldWb; #xlrd.book.Book object at 0x000000000315C940newWb =copy(oldWb);printnewWb; #xlwt.Workbook.Workbook object at 0x000000000315F470newWs =newWb.get_sheet(0);newWs.write(1, 0, "value1");newWs.write(1, 1, "value2");newWs.write(1, 2, "value3");print"write new values ok";newWb.save(gConst['xls']['fileName']);print"save with same name ok";
实现了,打开,刚刚保存的,已经存在的xls文件,
然后写入新数据的目的。
但是有个缺点,
第一次保存时的,带格式(标题内容为红色粗体)的内容:
重新写入新数据,再保存时,却丢失了之前的格式(标题没了红色粗体了):
11.后来还是参考:
writing to existing workbook using xlwt
中的那个标准答案,在用xlrd.open_workbook时,添加对应的参数formatting_info=True,就可以保留原有格式了。
完整代码:
?
1234567891011121314151617181920212223242526272829303132
importxlwt;importxlrd;#import xlutils;fromxlutils.copy importcopy; #init xls file#styleBlueBkg= xlwt.easyxf('pattern: pattern solid, fore_colour sky_blue;');#styleBold = xlwt.easyxf('font: bold on');styleBoldRed =xlwt.easyxf('font: color-index red, bold on');headerStyle =styleBoldRed;wb =xlwt.Workbook();ws =wb.add_sheet(gConst['xls']['sheetName']);ws.write(0, 0, "Header", headerStyle);ws.write(0, 1, "CatalogNumber", headerStyle);ws.write(0, 2, "PartNumber", headerStyle);wb.save(gConst['xls']['fileName']); #open existed xls file#newWb = xlutils.copy(gConst['xls']['fileName']);#newWb = copy(gConst['xls']['fileName']);oldWb =xlrd.open_workbook(gConst['xls']['fileName'], formatting_info=True);printoldWb; #xlrd.book.Book object at 0x000000000315C940newWb =copy(oldWb);printnewWb; #xlwt.Workbook.Workbook object at 0x000000000315F470newWs =newWb.get_sheet(0);newWs.write(1, 0, "value1");newWs.write(1, 1, "value2");newWs.write(1, 2, "value3");print"write new values ok";newWb.save(gConst['xls']['fileName']);print"save with same name ok";
?
1
最后重新写入的数据,就可以保留之前的格式了(标题为红色粗体):
总结
python中操作,本身就复杂的xls文件,还是有点小麻烦的。
想要,往已经存在的xls文件中,写入新的行,新的数据,对应的逻辑为:
用xlrd.open_workbook打开已有的xsl文件
注意添加参数formatting_info=True,得以保存之前数据的格式
然后用,from xlutils.copy import copy;,之后的copy去从打开的xlrd的Book变量中,拷贝出一份,成为新的xlwt的Workbook变量
然后对于xlwt的Workbook变量,就是正常的:
通过get_sheet去获得对应的sheet
拿到sheet变量后,就可以往sheet中,写入新的数据
写完新数据后,最终save保存
相关完整代码为:
?
12345678910111213141516171819202122232425262728 importxlwt;importxlrd;#import xlutils;fromxlutils.copy importcopy; styleBoldRed =xlwt.easyxf('font: color-index red, bold on');headerStyle =styleBoldRed;wb =xlwt.Workbook();ws =wb.add_sheet(gConst['xls']['sheetName']);ws.write(0, 0, "Header", headerStyle);ws.write(0, 1, "CatalogNumber", headerStyle);ws.write(0, 2, "PartNumber", headerStyle);wb.save(gConst['xls']['fileName']); #open existed xls file#newWb = xlutils.copy(gConst['xls']['fileName']);#newWb = copy(gConst['xls']['fileName']);oldWb =xlrd.open_workbook(gConst['xls']['fileName'], formatting_info=True);printoldWb; #xlrd.book.Book object at 0x000000000315C940newWb =copy(oldWb);printnewWb; #xlwt.Workbook.Workbook object at 0x000000000315F470newWs =newWb.get_sheet(0);newWs.write(1, 0, "value1");newWs.write(1, 1, "value2");newWs.write(1, 2, "value3");print"write new values ok";newWb.save(gConst['xls']['fileName']);print"save with same name ok";
菜鸟提问:如何让GridView的表头内容居中
列标题居中方法:
在前台(aspx文件)的“源”中建立类,如:
styletype="text/css"
.gridView_th
{
text-align:center;
}
/style
在设计中,选中gredview属性点开“HeaderStyle”在“CssClass”中输入类名:gridView_thok
注意:其他办法都没试通。
Python 操作Excel,怎么插入一行或一列
就是把插入行之后值重新输出来。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import xlwt;
import xlrd;
from xlutils.copy import copy;
#styleBoldRed = xlwt.easyxf('font: color-index red, bold on');
#headerStyle = styleBoldRed;
#wb = xlwt.Workbook();
#ws = wb.add_sheet('sheetName');
#ws.write(0, 0, "Col1", headerStyle);
#ws.write(0, 1, "Col2", headerStyle);
#ws.write(0, 2, "Col3", headerStyle);
#wb.save('fileName.xls');
#open existed xls file
oldWb = xlrd.open_workbook("fileName.xls", formatting_info=True);
oldWbS = oldWb.sheet_by_index(0)
newWb = copy(oldWb);
newWs = newWb.get_sheet(0);
inserRowNo = 1
newWs.write(inserRowNo, 0, "value1");
newWs.write(inserRowNo, 1, "value2");
newWs.write(inserRowNo, 2, "value3");
for rowIndex in range(inserRowNo, oldWbS.nrows):
for colIndex in range(oldWbS.ncols):
newWs.write(rowIndex + 1, colIndex, oldWbS.cell(rowIndex, colIndex).value);
newWb.save('fileName.xls');
print "save with same name ok";
用C#,将DataTable中的数据导出到Excel2007,求各位大神指导。。而且一定要有如下图的对话框进行提示。。
/// summary
/// 导出Excel
/// /summary
/// param name="dt"导出的数据源/param
/// param name="title"excel的名称/param
/// param name="fName"excel的标题/param
/// param name="resp"/param
public static void DataTableToExcel(DataTable dt, string title, string fName, HttpResponse resp)
{
ExcelEngine excelEngine = new ExcelEngine();
IApplicatiON application = excelEngine.Excel;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet sheet = workbook.Worksheets[0];
sheet.ImportDataTable(dt, true, 3, 1, -1, -1);
//Header Style
IStyle headerStyle = workbook.Styles.Add("HeaderStyle");
headerStyle.BeginUpdate();
//Add custom colors to the palette.
//workbook.SetPaletteColor(8, Color.FromArgb(182, 189, 218));
headerStyle.Color = Color.DarkBlue;
headerStyle.Font.Bold = true;
headerStyle.Font.Color = ExcelKnownColors.White;
headerStyle.Borders[ExcelBordersIndex.EdgeLeft].LineStyle = ExcelLineStyle.Thin;
headerStyle.Borders[ExcelBordersIndex.EdgeRight].LineStyle = ExcelLineStyle.Thin;
headerStyle.Borders[ExcelBordersIndex.EdgeTop].LineStyle = ExcelLineStyle.Thin;
headerStyle.Borders[ExcelBordersIndex.EdgeBottom].LineStyle = ExcelLineStyle.Thin;
//Apply Style
char[] constant = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z','a','b','c','d','e','f','g','h','i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r','s', 't', 'u', 'v', 'w', 'x', 'y', 'z'
};
sheet.Range["A1:" + constant[dt.Columns.Count - 1] + "1"].CellStyleName = "HeaderStyle";
sheet.Range["A3:" + constant[dt.Columns.Count - 1] + "3"].CellStyleName = "HeaderStyle";
headerStyle.EndUpdate();
//Autofit Rows and Columns
//sheet.UsedRange.AutofitRows();
sheet.UsedRange.AutofitColumns();
sheet.Range["A1"].Text = title;
sheet.Range["A1:" + constant[dt.Columns.Count - 1] + "1"].Merge();
sheet.Range["A1"].CellStyle.Font.Size = 10;
sheet.Range["A1"].CellStyle.HorizontalAlignment = ExcelHAlign.HAlignCenter;
//Saving the workbook to disk.
workbook.SaveAs(HttpUtility.UrlEncode(fName, Encoding.UTF8), ExcelSaveType.SaveAsXLS, resp, ExcelDownloadType.PromptDialog);
//No exception will be thrown if there are unsaved workbooks.
excelEngine.ThrowNotSavedOnDestroy = false;
excelEngine.Dispose();
}
headerstyle的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、headerstyle的信息别忘了在本站进行查找喔。
相关文章
发表评论
评论列表
- 这篇文章还没有收到评论,赶紧来抢沙发吧~