请问,VS2008 的VB.net导出Excel时,
1) 如何让单元格根据文字多少自动适应宽度.....
2) 如何设置单元格字体居中显示.....
我在网上找了很多例如
xlSheet.Range(xlSheet.Cells(1, 5), xlSheet.Cells(1, 8)).HorizontalAlignment = HorizontalAlignment.Center
xlSheet.Range("C2").HorizontalAlignment = HorizontalAlignment.Center
这种居中不能用,不起作用.......
求速度,在线等待,谢谢各位大侠.....
如何让vb.net导出excel使得单元格宽度自动适应??
答案:2 悬赏:30 手机版
解决时间 2021-02-26 16:34
- 提问者网友:树红树绿
- 2021-02-26 10:25
最佳答案
- 五星知识达人网友:何以畏孤独
- 2021-02-26 11:00
workbook.worksheets(1).Cells.EntireColumn.AutoFit() '自动调整Excel列
全部回答
- 1楼网友:七十二街
- 2021-02-26 11:30
我有现成的,你直接调用哈,没有时间给你看代码
public function daochu(byval x as datagridview) as boolean '导出到excel函数
try
if x.rows.count <= 0 then '判断记录数,如果没有记录就退出
messagebox.show("没有记录可以导出", "没有可以导出的项目", messageboxbuttons.ok, messageboxicon.information)
return false
else '如果有记录就导出到excel
dim xx as object : dim yy as object
xx = createobject("excel.application") '创建excel对象
yy = xx.workbooks.add()
dim i as integer, u as integer = 0, v as integer = 0 '定义循环变量,行列变量
for i = 1 to x.columns.count '把表头写入excel
yy.worksheets(1).cells(1, i) = x.columns(i - 1).headercell.value
next
dim str(x.rows.count - 1, x.columns.count - 1) '定义一个二维数组
for u = 1 to x.rows.count '行循环
for v = 1 to x.columns.count '列循环
if x.item(v - 1, u - 1).value.gettype.tostring <> "system.guid" then
str(u - 1, v - 1) = x.item(v - 1, u - 1).value
else
str(u - 1, v - 1) = x.item(v - 1, u - 1).value.tostring
end if
next
next
yy.worksheets(1).range("a2").resize(x.rows.count, x.columns.count).value = str '把数组一起写入excel
yy.worksheets(1).cells.entirecolumn.autofit() '自动调整excel列
' yy.worksheets(1).name = x.topleftheadercell.value.tostring '表标题写入作为excel工作表名称
xx.visible = true '设置excel可见
yy = nothing '销毁组建释放资源
xx = nothing '销毁组建释放资源
end if
return true
catch ex as exception '错误处理
messagebox.show(err.description.tostring, "错误", messageboxbuttons.ok, messageboxicon.error) '出错提示
return false
end try
end function
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯