如何使用VB.NET自定义Excel [英] How to customize excel using VB.NET

查看:236
本文介绍了如何使用VB.NET自定义Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好早上好

我在VB.Net中有一个程序,它将把数据从Mysql填充到Datagridview。

我还有一个名为Export的按钮它将以Excel格式导出Datagridview数据。





http://s32.postimg.org/xr4suyzz9/image.jpg [ ^ ]



但我的教授喜欢这种格式。

< br $> b $ b

http://s32.postimg.org/e9x4bqlyd/image .jpg [ ^ ]





我怎样才能实现这一目标?喜欢



1.放一个中心标题



2.将一个.00放在最后数字列数



3.找到列中的最后一个单元格并求它。



我希望有人能帮助我





这是我在导出按钮中的代码



 如果 DataGridView1.Rows.Count =  0  < span class =code-keyword>然后 
MsgBox( 无法导出
其他
Dim ExcelApp 作为 对象,ExcelBook 作为 对象
Dim ExcelSheet As 对象
Dim i As 整数
Dim j As 整数

ExcelApp = CreateObject( Excel.Application
ExcelBook = ExcelApp.WorkBooks.Add
ExcelSheet = ExcelBook.WorkSheets( 1
使用 ExcelSheet
对于 每个作为 DataGridViewColumn DataGridView1.Columns
.cells( 1 ,column.Index + 1 )= column.HeaderText
下一步
对于 i = 1 .DataGridView1.RowCount
.cells(i + 1 1 )= .DataGridView1.Rows(i - 1 )。单元格( ItemCode)。值
对于 j = 1 DataGridView1.Columns.Count - 1
.cells(i + 1 ,j + 1 )= DataGridView1.Rows(i - 1 ).Cells(j).Value

下一步


下一步
结束 使用
ExcelApp.Visible = True
ExcelSheet = Nothing
ExcelBook = Nothing
ExcelApp = Nothing

结束 如果





我的尝试:



截至目前没有发生任何事情我尝试了Everuthing

解决方案

你错了,你的2张图片没有显示相同的数据,我们无法用此设备任何东西。

建议:仅在图片问题中使用图片,而不是文字。输入数据示例。

解释目标和代码中的错误(不要说它不起作用,我们已经知道了)。 这个x位置错误,这个y需要2位小数



如果你可以直接在excel中做spmething,请使用宏录制器查看Excel用VB翻译你的动作。



你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



使用调试器查看你的代码在做什么。



引用:

我试过Everuthing 我可以向你保证你没有


Hello Everyone Good Morning
I have a Program in VB.Net that will Populate Data from Mysql into the Datagridview.
and I have also a button called Export and It will Export Datagridview Data in Excel Format like this.


http://s32.postimg.org/xr4suyzz9/image.jpg[^]

but my our Prof. like this Format.


http://s32.postimg.org/e9x4bqlyd/image.jpg[^]


How can I achive that? like

1. Put a Center Header

2. Put a .00 at the End of the Number of a Number Column

3. Find the Last Cell in a Column and Sum It.

I hope someone would help me


Here is my code in Export Button

If DataGridView1.Rows.Count = 0 Then
              MsgBox("Nothing to Export")
          Else
              Dim ExcelApp As Object, ExcelBook As Object
              Dim ExcelSheet As Object
              Dim i As Integer
              Dim j As Integer

              ExcelApp = CreateObject("Excel.Application")
              ExcelBook = ExcelApp.WorkBooks.Add
              ExcelSheet = ExcelBook.WorkSheets(1)
              With ExcelSheet
                  For Each column As DataGridViewColumn In DataGridView1.Columns
                      .cells(1, column.Index + 1) = column.HeaderText
                  Next
                  For i = 1 To Me.DataGridView1.RowCount
                      .cells(i + 1, 1) = Me.DataGridView1.Rows(i - 1).Cells("ItemCode").Value
                      For j = 1 To DataGridView1.Columns.Count - 1
                          .cells(i + 1, j + 1) = DataGridView1.Rows(i - 1).Cells(j).Value

                      Next


                  Next
              End With
              ExcelApp.Visible = True
              ExcelSheet = Nothing
              ExcelBook = Nothing
              ExcelApp = Nothing

          End If



What I have tried:

As of Now Nothing Happens and I tried Everuthing

解决方案

you wrong, your 2 pictures do not show the same data, we can't device any thing with this.
Advice: use pictures in questions only for graphics, not fir text. Type the examples of data.
Explain the goal and what is wrong in your code(don't say it is not working, we already know it). "this x is on wrong position", "this y needs 2 decimals"
.
If you can do spmething directly in excel, use the macro recorder to see how Excel translate your actions in VB.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

Use the debugger to see what your code is doing.

Quote:

I tried Everuthing

I can insure you that you didn't


这篇关于如何使用VB.NET自定义Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆