简体   繁体   English

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

[英]vb.net export to excel change cell format

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. 在Web应用程序中显示标准格式。 But when I convert it into excel, it displays: 999 (due to cell format = General) 但是当我将其转换为excel时,它会显示:999(由于格式=通用)

Anything I can do to change the cell format to Number? 我能做些什么来将单元格格式更改为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(但它也改为字符串而不是数字并对齐到左侧,这不是一个好的显示)

It seems that you are in search of Range.NumberFormat Property. 您似乎正在搜索Range.NumberFormat属性。 This snippet can help you out: 此代码段可以帮助您:

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

For more help check out this one. 如需更多帮助,请查看此内容

NeverHopeless is giving you information about how to use a macro in VBA to format the column in Excel. NeverHopeless为您提供有关如何在VBA中使用宏来格式化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. 但是,我怀疑你是否希望excel用户能够使用空白的Excel文档直接将新值直接输入excel,或者有办法从网站快速打开excel中的格式化值。 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. 不幸的是,正如尝试从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. 请务必使用能够控制数字格式的文件格式,如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. 如果这是您想要的格式,您可以利用服务器端的Excel API来帮助创建Excel文件。 Then you could control coloring, and all sorts of things besides just number format. 然后你可以控制着色,除了数字格式之外还有各种各样的东西。

Using the Excel Interop library: 使用Excel Interop库:

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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM