以文本方式查看主题

-  Foxtable(狐表)  (http://www.foxtable.com/bbs/index.asp)
--  专家坐堂  (http://www.foxtable.com/bbs/list.asp?boardid=2)
----  [求助]按帮助的例子设置Excel背景色后表格线没了、日期格式变成了数字  (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=53553)

--  作者:shenyl0211
--  发布时间:2014/7/10 14:44:00
--  [求助]按帮助的例子设置Excel背景色后表格线没了、日期格式变成了数字

1、根据Excel模板生成了Excel表格,但背景色没有了。于是

2、根据帮助的开发指南——Excel报表——报表编程——导出数据——按例子的设置背景色部分设置背景色后,只要有背景色的地方,原有的表格线没了,原有的日期也变成了数字——怎么办?


--  作者:有点甜
--  发布时间:2014/7/10 14:58:00
--  

 设置一下单元格的样式之前,先拷贝对应样式

 

 Dim Book As New XLS.Book \'定义一个Excel工作簿
Dim Sheet As XLS.Sheet = Book.Sheets(0) \'引用工作簿的第一个工作表
Dim Style As Xls.Style = Book.NewStyle \'新建一个样式
style.Format = sheet(0.0).Style.Format
style.BackColor = Color.Red
sheet(0.0).Style = style


--  作者:shenyl0211
--  发布时间:2014/7/10 17:05:00
--  

谢谢甜,但不行,那不又回到设置前的了?因为只允许设置一种样式,否则就被替换为默认的

我参考附录中的“Excel与VBA”解决了。代码也不多:

            Book.Save(file1) \'先保存
            Dim App As New MSExcel.Application
            Dim Wb As MSExcel.WorkBook = App.WorkBooks.Open(file1) ‘再打开
            Dim Ws As MSExcel.WorkSheet = Wb.WorkSheets(1)
            Ws.Range(dr0).NumberFormat = "yyyy-mm-dd" \'日期 ’设置日期格式,范围变量dr0先期赋值
            Dim Rg As MSExcel.Range = Ws.UsedRange
            With Rg.Borders(MSExcel.XlBordersIndex.xlInsideHorizontal) \'设置被替换的水平表格线
                .LineStyle = MSExcel.XlLineStyle.xlContinuous
                .Weight = MSExcel.XlBorderWeight.xlThin
            End With
            With Rg.Borders(MSExcel.XlBordersIndex.xlInsideVertical) ‘设置被替换的垂直网格线
                .LineStyle = MSExcel.XlLineStyle.xlContinuous
                .Weight = MSExcel.XlBorderWeight.xlThin
            End With
            Wb.Save
            App.Visible = True

 

[此贴子已经被作者于2014-7-10 19:02:32编辑过]