zoukankan      html  css  js  c++  java
  • VB.NET导出Excel报表时格式化单元格

    在使用VB.NET 导出Excel报表时,Excel并没有按照数据库的格式原样输出。如,075,0078等数据在Excel 中会被显示为75,78。对于显示在Excel中的数据,如果没有特别指定数据格式,Excel 会按照默认的数据类型格式化数据。所以,对于075,0078等数据单元格的数据格式需要在代码中特别指定。

    Dim sstyle AsString
    Dim LBPColumnNumber As Integer = 11
    sstyle ="<style> .text { mso-number-format:\@; } </style> "
    Response.Write(sstyle)
    ForEach row As GridViewRow In gvExport.Rows
    row.Cells(LBPColumnNumber).Attributes.Add(
    "Class", "text")
    Next

    格式化单元格参考:(摘自网络)

    mso-number-format:"0" NO Decimals
    mso-number-format:"0\.000" 3 Decimals
    mso-number-format:"\#\,\#\#0\.000" Comma with 3 dec
    mso-number-format:"mm\/dd\/yy" Date7
    mso-number-format:"mmmm\ d\,\ yyyy" Date9
    mso-number-format:"m\/d\/yy\ h\:mm\ AM\/PM" D -T AMPM
    mso-number-format:"Short Date" 01/03/1998
    mso-number-format:"Medium Date" 01-mar-98
    mso-number-format:"d\-mmm\-yyyy" 01-mar-1998
    mso-number-format:"Short Time" 5:16
    mso-number-format:"Medium Time" 5:16 am
    mso-number-format:"Long Time" 5:16:21:00
    mso-number-format:"Percent" Percent - two decimals
    mso-number-format:"0%" Percent - no decimals
    mso-number-format:"0\.E+00" Scientific Notation
    mso-number-format:"\@" Text
    mso-number-format:"\#\ ???\/???" Fractions - up to 3 digits (312/943)
    mso-number-format:"\0022£\0022\#\,\#\#0\.00" £12.76
    mso-number-format:"\#\,\#\#0\.00_ \;\[Red\]\-\#\,\#\#0\.00\ " 2 decimals, negative numbers in red and signed
    (1.56 -1.56)

    当我们用<%@page contentType="application/vnd.ms-excel; charset=UTF-8"%>的方法导出html文件为excel时,如果导出的数据中有数字以0开头,则该0会被省略,为了保留这个处于首位的 0,可以在表格的style中加入:
    style='mso-number-format:"\@";'

    然后,再将这个样式写入到html的流中,这样0就能保留下来。

  • 相关阅读:
    Bootstrap学习
    Bootstrap学习
    Windows下Apache+Django+mod_wsgi的static和media问题处理
    Windows编译安装mod_wsgi,配合使用Django+Apahce
    Bootstrap学习
    Chapter 21_4 捕获
    Chapter 21_3 模式
    新发现的一些C函数
    Chapter 21_2 模式匹配函数
    Chapter 21_1 字符串函数
  • 原文地址:https://www.cnblogs.com/Dannier/p/1978632.html
Copyright © 2011-2022 走看看