vb.net 导出到 excel 更改单元格格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17645962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:15:36  来源:igfitidea点击:

vb.net export to excel change cell format

.netvb.netexport-to-excel

提问by Feliks

I am currently needed to show data in a standard format in excel converted from a web application.

我目前需要在从 Web 应用程序转换的 excel 中以标准格式显示数据。

Number format needed to be displayed: 999.00

需要显示的数字格式:999.00

I use:

我用:

 CDbl(number).ToString("#,##0.00") 

to display standard format in the web application. But when I convert it into excel, it displays: 999 (due to cell format = General)

在 Web 应用程序中显示标准格式。但是当我把它转换成excel时,它显示:999(由于单元格格式=常规)

Anything I can do to change the cell format to Number?

我可以做些什么来将单元格格式更改为数字?

I tried add a spacebar in between to display the 2 decimal points:

我尝试在中间添加一个空格键以显示 2 个小数点:

CDbl(number).ToString("#,##0. 00")

It displays: 999. 00 (but it also changed to string instead of number and aligned to left which is not a good display)

它显示: 999. 00 (但它也变成了字符串而不是数字并且向左对齐,这不是一个很好的显示)

回答by NeverHopeless

It seems that you are in search of Range.NumberFormatProperty. This snippet can help you out:

您似乎在寻找Range.NumberFormat属性。此代码段可以帮助您:

range.Cells(0,2).EntireColumn.NumberFormat = "@";

For more help check out thisone.

如需更多帮助,请查看这个

回答by Ted

NeverHopeless is giving you information about how to use a macro in VBA to format the column in Excel. However, my suspicion is that you want excel users to be able to use a blank excel document to simply pull in new values directly into excel, or have a way to quickly open the formatted values in excel from the website. Unfortunately, as is evidenced by trying to load values from a csv file format, raw values do not contain the formatting information you need in excel.

NeverHopeless 为您提供有关如何在 VBA 中使用宏来格式化 Excel 中的列的信息。但是,我的怀疑是您希望 excel 用户能够使用空白的 excel 文档将新值直接拉入 excel,或者有一种方法可以从网站快速打开 excel 中的格式化值。不幸的是,正如尝试从 csv 文件格式加载值所证明的那样,原始值不包含您在 excel 中需要的格式信息。

One option would be to host an "export" button on the website to allow users to download a file from the website that is created on the fly based on the data values in the table and that has the formatting that excel needs. Be sure to use a file format that has the power to control the number format like xlsx, xls, xml, sylk, dif or something other than say csv which only has raw number values.

一种选择是在网站上托管一个“导出”按钮,以允许用户从网站下载基于表格中的数据值动态创建的文件,该文件具有 excel 所需的格式。请务必使用能够控制数字格式的文件格式,例如 xlsx、xls、xml、sylk、dif 或仅包含原始数字值的 csv 以外的其他格式。

You can probably take advantage of the Excel API on the server side to help create an excel file if that's the format you want. Then you could control coloring, and all sorts of things besides just number format.

如果这是您想要的格式,您可能可以利用服务器端的 Excel API 来帮助创建 Excel 文件。然后你可以控制着色,以及除了数字格式之外的各种事情。

Using the Excel Interop library:

使用 Excel 互操作库:

Dim style as Microsoft.Office.Interop.Excel.Style
style.NumberFormat = "#,##0.00"

With a reference to the range:

参考范围:

myRange.NumberFormat = "#,##0.00"

The current selection is also a range, so if you want it to work with the selection, just use:

当前选择也是一个范围,因此如果您希望它与选择一起使用,只需使用:

Selection.NumberFormat = "#,##0.00"