NPOIc npoi 导出excell表 如何实现短日期格式

温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
阅读(2455)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
loftPermalink:'',
id:'fks_',
blogTitle:'.NET使用NPOI组件将数据导出Excel',
blogAbstract:'
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}NPOI&导出excel带图片,可控大小
using NPOI.HSSF.UserM
using NPOI.HSSF.U
using NPOI.DDF;
using NPOI.SS.UserM
using System.IO;
using NPOI.SS;&&
#region 导出
protected void btnexcel_Click(object sender, EventArgs e)
&&&&&&&&&&&
QQT_BLL.gg_content bll = new QQT_BLL.gg_content();
&&&&&&&&&&&
DataSet ds = bll.getds("");
&&&&&&&&&&&
DataTable dt = ds.Tables[0];
&&&&&&&&&&&
string excelname = System.DateTime.Now.ToString().Replace(":",
"").Replace("-", "").Replace(" ", "");
&&&&&&&&&&&
string filePath =
System.Web.HttpContext.Current.Server.MapPath("ReadExcel") + "\\" +
excelname + ".xls";
&&&&&&&&&&&
MemoryStream ms = RenderDataTableToExcel(dt) as MemoryS
&&&&&&&&&&&
FileStream fs = new FileStream(filePath, FileMode.Create,
FileAccess.Write);
&&&&&&&&&&&
byte[] data = ms.ToArray();
&&&&&&&&&&&
fs.Write(data, 0, data.Length);
&&&&&&&&&&&
fs.Flush();
&&&&&&&&&&&
fs.Close();
&&&&&&&&&&&
&&&&&&&&&&&
&&&&&&&&&&&
&&&&&&&&&&&
#region 导出到客户端
&&&&&&&&&&&
Response.ContentEncoding =
System.Text.Encoding.GetEncoding("GB2312");
&&&&&&&&&&&
Response.AppendHeader("content-disposition", "filename="
+ System.Web.HttpUtility.UrlEncode(excelname,
System.Text.Encoding.UTF8) + ".xls");
&&&&&&&&&&&
Response.ContentType = "Application/excel";
&&&&&&&&&&&
Response.WriteFile(filePath);
&&&&&&&&&&&
Response.End();
&&&&&&&&&&&
#endregion
public Stream RenderDataTableToExcel(DataTable SourceTable)
&&&&&&&&&&&
MemoryStream ms = new MemoryStream();
&&&&&&&&&&&
NPOI.HSSF.UserModel.HSSFWorkbook workbook = new
NPOI.HSSF.UserModel.HSSFWorkbook();
&&&&&&&&&&&
NPOI.SS.UserModel.ISheet sheet = workbook.CreateSheet();
&&&&&&&&&&&
NPOI.SS.UserModel.IRow headerRow = sheet.CreateRow(0);
//设置7列宽为100
&&&&&&&&&&&
sheet.SetColumnWidth(7, 100);
&&&&&&&&&&&
headerRow.CreateCell(0).SetCellValue("广告编号");
&&&&&&&&&&&
headerRow.CreateCell(1).SetCellValue("广告标题");
&&&&&&&&&&&
headerRow.CreateCell(2).SetCellValue("广告内容");
&&&&&&&&&&&
headerRow.CreateCell(3).SetCellValue("所属广告商");
&&&&&&&&&&&
headerRow.CreateCell(4).SetCellValue("广告电话");
&&&&&&&&&&&
headerRow.CreateCell(5).SetCellValue("广告网址");
&&&&&&&&&&&
headerRow.CreateCell(6).SetCellValue("广告小图片");
&&&&&&&&&&&
headerRow.CreateCell(7).SetCellValue("图片显示");
&&&&&&&&&&&
headerRow.CreateCell(8).SetCellValue("广告大图片");
&&&&&&&&&&&
headerRow.CreateCell(9).SetCellValue("图片显示");
&&&&&&&&&&&
headerRow.CreateCell(10).SetCellValue("审核状态");
&&&&&&&&&&&
int rowIndex = 1;
&&&&&&&&&&&
foreach (DataRow row in SourceTable.Rows)
&&&&&&&&&&&
&&&&&&&&&&&&&&&
NPOI.SS.UserModel.IRow dataRow = sheet.CreateRow(rowIndex);
&&&&&&&&&&&&&&&
dataRow.Height = 50;
&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&
dataRow.CreateCell(0).SetCellValue(row["gg_id"].ToString());
&&&&&&&&&&&&&&&
dataRow.CreateCell(1).SetCellValue(row["gg_title"].ToString());
&&&&&&&&&&&&&&&
dataRow.CreateCell(2).SetCellValue(row["gg_memo"].ToString());
&&&&&&&&&&&&&&&
dataRow.CreateCell(3).SetCellValue(row["ggs_name"].ToString());
&&&&&&&&&&&&&&&
dataRow.CreateCell(4).SetCellValue(row["gg_tel"].ToString());
&&&&&&&&&&&&&&&
dataRow.CreateCell(5).SetCellValue(row["gg_add"].ToString());
&&&&&&&&&&&&&&&
dataRow.CreateCell(6).SetCellValue(row["p_name"].ToString());
&&&&&&&&&&&&&&&
string picurl =
row["p_url"].ToString();&&//图片存储路径&&&&&&&&&&&&&
&&&&&&&&&&&&&&&
dataRow.CreateCell(8).SetCellValue(row["gg_check"].ToString());
&&&&&&&&&&&&&&&
AddPieChart(sheet,& workbook, picurl,rowIndex
&&&&&&&&&&&&&&&
rowIndex++;
&&&&&&&&&&&
&&&&&&&&&&&
workbook.Write(ms);
&&&&&&&&&&&
ms.Flush();
&&&&&&&&&&&
ms.Position = 0;
&&&&&&&&&&&
&&&&&&&&&&&
headerRow =
&&&&&&&&&&&
workbook =
&&&&&&&&&&&
/// 向sheet插入图片
private void AddPieChart(ISheet
HSSFWorkbook& workbook, string fileurl,int row,int
&&&&&&&&&&&
&&&&&&&&&&&
&&&&&&&&&&&&&&&
//add picture data to this workbook.
&&&&&&&&&&&&&&&
string path = Server.MapPath("~/html/");
&&&&&&&&&&&&&&&
if (fileurl.Contains("/"))
&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&
path += fileurl.Substring( fileurl.IndexOf ('/'));
&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&
string FileName =
&&&&&&&&&&&&&&&
byte[] bytes = System.IO.File.ReadAllBytes(FileName);
&&&&&&&&&&&&&&&
if (!string.IsNullOrEmpty(FileName))
&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&
int pictureIdx = workbook.AddPicture(bytes,NPOI .SS .UserModel
.PictureType.JPEG);&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&
HSSFPatriarch patriarch =
(HSSFPatriarch)sheet.CreateDrawingPatriarch();
&&&&&&&&&&&&&&&&&&&
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 100, 50, col ,
row , col +1, row +1);
&&&&&&&&&&&&&&&&&&&
//##处理照片位置,【图片左上角为(col, row)第row+1行col+1列,右下角为( col +1, row +1)第
col +1+1行row +1+1列,宽为100,高为50
&&&&&&&&&&&&&&&&&&&
HSSFPicture pict = (HSSFPicture)patriarch.CreatePicture(anchor,
pictureIdx);
&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&//
pict.Resize();这句话一定不要,这是用图片原始大小来显示
&&&&&&&&&&&&&&&
&&&&&&&&&&&
&&&&&&&&&&&
catch (Exception ex)
&&&&&&&&&&&
&&&&&&&&&&&&&&&
&&&&&&&&&&&
#endregion
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。POI设置EXCEL单元格格式为文本、小数、百分比、货币、日期、科学计数法和中文大写 - JavaCrazyer的)技术博客 - ITeye技术网站
博客分类:
再读本篇文章之前,请先看我的前一篇,前一篇中有重点讲到POI设置EXCEL单元格格式为文本格式,剩下的设置小数、百分比、货币、日期、科学计数法和中文大写这些将在下面一一写出
以下将要介绍的每一种都会用到这三行中的变量
HSSFWorkbook demoWorkBook = new HSSFWorkbook();
HSSFSheet demoSheet = demoWorkBook.createSheet("The World's 500 Enterprises");
HSSFCell cell = demoSheet.createRow(0).createCell(0);
第一种:日期格式
cell.setCellValue(new Date());
//set date format
HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();
HSSFDataFormat format= demoWorkBook.createDataFormat();
cellStyle.setDataFormat(format.getFormat("yyyy年m月d日"));
cell.setCellStyle(cellStyle);
第二种:保留两位小数格式
cell.setCellValue(1.2);
HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));
cell.setCellStyle(cellStyle);
这里与上面有所不同,用的是HSSFDataFormat.getBuiltinFormat()方法,之所以用这个,是因为0.00是Excel内嵌的格式,完整的Excel内嵌格式列表大家可以看这个窗口中的自定义列表:
这里就不一一列出了
第三种:货币格式
cell.setCellValue(20000);
HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();
HSSFDataFormat format= demoWorkBook.createDataFormat();
cellStyle.setDataFormat(format.getFormat("?#,##0"));
cell.setCellStyle(cellStyle);
第四种:百分比格式
cell.setCellValue(20);
HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00%"));
cell.setCellStyle(cellStyle);
此种情况跟第二种一样
第五种:中文大写格式
cell.setCellValue(20000);
HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();
HSSFDataFormat format= demoWorkBook.createDataFormat();
cellStyle.setDataFormat(format.getFormat("[DbNum2][$-804]0"));
cell.setCellStyle(cellStyle);
第六种:科学计数法格式
cell.setCellValue(20000);
HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();
cellStyle.setDataFormat( HSSFDataFormat.getBuiltinFormat("0.00E+00"));
cell.setCellStyle(cellStyle);
此种情况也与第二种情况一样
浏览 43210
JavaCrazyer
浏览: 2355729 次
来自: 河南
楼主,看你的博客学习了很多,文章写的都蛮好的。但是为什么这个并 ...
好,32个赞送你
[color=yellow][/co444lor]&&国之画&&&& &&&&&&
&& &&&&&&&&&&&&&&
鲁ICP备号-4
打开技术之扣,分享程序人生!使用NPOI操作Excel文件及其日期处理 - aolinwxfx - 推酷
使用NPOI操作Excel文件及其日期处理 - aolinwxfx
工作中经常遇到需要读取或导出Excel文件的情况,而NPOI是目前最宜用、效率最高的操作的Office(不只是Excel哟)文件的组件,使用方便,不详细说明了。
Excel工作表约定:
整个Excel表格叫做工作表:WorkBook(工作薄),包含的叫页(工作表):Sheet;行:Row;单元格Cell。
1、先上一个Excel2003的例子,代码如下:
//读取xls文件
&&&&&&& private void button2_Click(object sender, EventArgs e)
&&&&&&& {&& StringBuilder sbr = new StringBuilder();
&&&&&&&&&&& using (FileStream fs = File.OpenRead(@&c:/myxls.xls&))&& //打开myxls.xls文件
&&&&&&&&&&& {
&&&&&&&&&&&&&&& HSSFWorkbook wk = new HSSFWorkbook(fs);&& //把xls文件中的数据写入wk中
&&&&&&&&&&&&&&& for (int i = 0; i & wk.NumberOfS i++)& //NumberOfSheets是myxls.xls中总共的表数
&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&& ISheet sheet = wk.GetSheetAt(i);&& //读取当前表数据
&&&&&&&&&&&&&&&&&&& for (int j = 0; j &= sheet.LastRowN j++)& //LastRowNum 是当前表的总行数
&&&&&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&&&&&& IRow row = sheet.GetRow(j);& //读取当前行数据
&&&&&&&&&&&&&&&&&&&&&&& if (row != null)
&&&&&&&&&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&&&&&&&&&& sbr.Append(&-------------------------------------\r\n&); //读取行与行之间的提示界限
&&&&&&&&&&&&&&&&&&&&&&&&&&& for (int k = 0; k &= row.LastCellN k++)& //LastCellNum 是当前行的总列数
&&&&&&&&&&&&&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& ICell cell = row.GetCell(k);& //当前表格
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& if (cell != null)
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& sbr.Append(cell.ToString());&& //获取表格中的数据并转换为字符串类型
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&&
&&&&&&&&&&& }
&&&&&&&&&&& sbr.ToString();
&&&&&&&&&&& using (StreamWriter wr = new StreamWriter(new FileStream(@&c:/myText.txt&, FileMode.Append)))& //把读取xls文件的数据写入myText.txt文件中
&&&&&&&&&&& {
&&&&&&&&&&&&&&& wr.Write(sbr.ToString());
&&&&&&&&&&&&&&& wr.Flush();
&&&&&&&&&&& }
//创建一个常用的xls文件
&&&&&&& private void button3_Click(object sender, EventArgs e)
&&&&&&& {&&&&&&&&&&
&&&&&&&&&&& IWorkbook wb = new HSSFWorkbook();
&&&&&&&&&&& //创建表&&
&&&&&&&&&&& ISheet sh = wb.CreateSheet(&zhiyuan&);
&&&&&&&&&&& //设置单元的宽度&&
&&&&&&&&&&& sh.SetColumnWidth(0, 15 * 256);
&&&&&&&&&&& sh.SetColumnWidth(1, 35 * 256);
&&&&&&&&&&& sh.SetColumnWidth(2, 15 * 256);
&&&&&&&&&&& sh.SetColumnWidth(3, 10 * 256);
&&&&&&&&&&& int i = 0;
&&&&&&&&&&& #region 练习合并单元格
&&&&&&&&&&& sh.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 3));
&&&&&&&&&&& //CellRangeAddress()该方法的参数次序是:开始行号,结束行号,开始列号,结束列号。
&&&&&&&&&&& IRow row0 = sh.CreateRow(0); &&&&&&&&&&& row0.Height = 20 * 20; &&&&&&&&&&& ICell icell1top0 = row0.CreateCell(0); &&&&&&&&&&& icell1top0.CellStyle = Getcellstyle(wb, stylexls.头); &&&&&&&&&&& icell1top0.SetCellValue(&标题合并单元&); &&&&&&&&&&& #endregion &&&&&&&&&&& i++; &&&&&&&&&&& #region 设置表头 &&&&&&&&&&& IRow row1 = sh.CreateRow(1); &&&&&&&&&&& row1.Height = 20 * 20;
&&&&&&&&&&& ICell icell1top = row1.CreateCell(0); &&&&&&&&&&& icell1top.CellStyle = Getcellstyle(wb, stylexls.头); &&&&&&&&&&& icell1top.SetCellValue(&网站名&);
&&&&&&&&&&& ICell icell2top = row1.CreateCell(1); &&&&&&&&&&& icell2top.CellStyle = Getcellstyle(wb, stylexls.头); &&&&&&&&&&& icell2top.SetCellValue(&网址&);
&&&&&&&&&&& ICell icell3top = row1.CreateCell(2); &&&&&&&&&&& icell3top.CellStyle = Getcellstyle(wb, stylexls.头); &&&&&&&&&&& icell3top.SetCellValue(&百度快照&);
&&&&&&&&&&& ICell icell4top = row1.CreateCell(3); &&&&&&&&&&& icell4top.CellStyle = Getcellstyle(wb, stylexls.头); &&&&&&&&&&& icell4top.SetCellValue(&百度收录&); &&&&&&&&&&& #endregion&& &&&&&&&&&&& using(FileStream stm=File.OpenWrite(@&c:/myMergeCell.xls&)) &&&&&&&&&&& { &&&&&&&&&&&&&&& wb.Write(stm);& &&&&&&&&&&&&&&& MessageBox.Show(&提示:创建成功!&); &&&&&&&&&&& } &&&&&&& }&
&&&&&&& #region 定义单元格常用到样式的枚举 &&&&&&& public enum stylexls &&&&&&& { &&&&&&&&&&& 头, &&&&&&&&&&& url, &&&&&&&&&&& 时间, &&&&&&&&&&& 数字, &&&&&&&&&&& 钱, &&&&&&&&&&& 百分比, &&&&&&&&&&& 中文大写, &&&&&&&&&&& 科学计数法, &&&&&&&&&&& 默认 &&&&&&& } &&&&&&& #endregion
&&&&&&& #region 定义单元格常用到样式 &&&&&&& static ICellStyle Getcellstyle(IWorkbook wb, stylexls str) &&&&&&& { &&&&&&&&&&& ICellStyle cellStyle = wb.CreateCellStyle();
&&&&&&&&&&& //定义几种字体&& &&&&&&&&&&& //也可以一种字体,写一些公共属性,然后在下面需要时加特殊的&& &&&&&&&&&&& IFont font12 = wb.CreateFont(); &&&&&&&&&&& font12.FontHeightInPoints = 10; &&&&&&&&&&& font12.FontName = &微软雅黑&;
&&&&&&&&&&& IFont font = wb.CreateFont(); &&&&&&&&&&& font.FontName = &微软雅黑&; &&&&&&&&&&& //font.Underline = 1;下划线&
&&&&&&&&&&& IFont fontcolorblue = wb.CreateFont(); &&&&&&&&&&& fontcolorblue.Color = HSSFColor.OLIVE_GREEN.BLUE. &&&&&&&&&&& fontcolorblue.IsItalic =//下划线&& &&&&&&&&&&& fontcolorblue.FontName = &微软雅黑&;
&&&&&&&&&&& //边框&& &&&&&&&&&&& cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.DOTTED; &&&&&&&&&&& cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.HAIR; &&&&&&&&&&& cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.HAIR; &&&&&&&&&&& cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.DOTTED; &&&&&&&&&&& //边框颜色&& &&&&&&&&&&& cellStyle.BottomBorderColor = HSSFColor.OLIVE_GREEN.BLUE. &&&&&&&&&&& cellStyle.TopBorderColor = HSSFColor.OLIVE_GREEN.BLUE.
&&&&&&&&&&& //背景图形,我没有用到过。感觉很丑&& &&&&&&&&&&& //cellStyle.FillBackgroundColor = HSSFColor.OLIVE_GREEN.BLUE.&& &&&&&&&&&&& //cellStyle.FillForegroundColor = HSSFColor.OLIVE_GREEN.BLUE.&& &&&&&&&&&&& cellStyle.FillForegroundColor = HSSFColor.WHITE. &&&&&&&&&&& // cellStyle.FillPattern = FillPatternType.NO_FILL;&& &&&&&&&&&&& cellStyle.FillBackgroundColor = HSSFColor.BLUE.
&&&&&&&&&&& //水平对齐&& &&&&&&&&&&& cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.LEFT;
&&&&&&&&&&& //垂直对齐&& &&&&&&&&&&& cellStyle.VerticalAlignment = VerticalAlignment.CENTER;
&&&&&&&&&&& //自动换行&& &&&&&&&&&&& cellStyle.WrapText =
&&&&&&&&&&& //缩进;当设置为1时,前面留的空白太大了。希旺官网改进。或者是我设置的不对&& &&&&&&&&&&& cellStyle.Indention = 0;
&&&&&&&&&&& //上面基本都是设共公的设置&&
&&&&&&&&&&& //下面列出了常用的字段类型&& &&&&&&&&&&& switch (str) &&&&&&&&&&& { &&&&&&&&&&&&&&& case stylexls.头: &&&&&&&&&&&&&&&&&&& // cellStyle.FillPattern = FillPatternType.LEAST_DOTS;&& &&&&&&&&&&&&&&&&&&& cellStyle.SetFont(font12); &&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&& case stylexls.时间: &&&&&&&&&&&&&&&&&&& IDataFormat datastyle = wb.CreateDataFormat();
&&&&&&&&&&&&&&&&&&& cellStyle.DataFormat = datastyle.GetFormat(&yyyy/mm/dd&); &&&&&&&&&&&&&&&&&&& cellStyle.SetFont(font); &&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&& case stylexls.数字: &&&&&&&&&&&&&&&&&&& cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat(&0.00&); &&&&&&&&&&&&&&&&&&& cellStyle.SetFont(font); &&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&& case stylexls.钱: &&&&&&&&&&&&&&&&&&& IDataFormat format = wb.CreateDataFormat(); &&&&&&&&&&&&&&&&&&& cellStyle.DataFormat = format.GetFormat(&¥#,##0&); &&&&&&&&&&&&&&&&&&& cellStyle.SetFont(font); &&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&& case stylexls.url: &&&&&&&&&&&&&&&&&&& fontcolorblue.Underline = 1; &&&&&&&&&&&&&&&&&&& cellStyle.SetFont(fontcolorblue); &&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&& case stylexls.百分比: &&&&&&&&&&&&&&&&&&& cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat(&0.00%&); &&&&&&&&&&&&&&&&&&& cellStyle.SetFont(font); &&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&& case stylexls.中文大写: &&&&&&&&&&&&&&&&&&& IDataFormat format1 = wb.CreateDataFormat(); &&&&&&&&&&&&&&&&&&& cellStyle.DataFormat = format1.GetFormat(&[DbNum2][$-804]0&); &&&&&&&&&&&&&&&&&&& cellStyle.SetFont(font); &&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&& case stylexls.科学计数法: &&&&&&&&&&&&&&&&&&& cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat(&0.00E+00&); &&&&&&&&&&&&&&&&&&& cellStyle.SetFont(font); &&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&& case stylexls.默认: &&&&&&&&&&&&&&&&&&& cellStyle.SetFont(font); &&&&&&&&&&&&&&&&&&& &&&&&&&&&&& } &&&&&&&&&&& return cellS
&&&&&&& } &&&&&&& #endregion
2、再来一个Excel2007的并
绑定到GridView中
例子,代码如下:
using System.Collections.G
using System.L
using System.W
using System.D
using System.IO;
using NPOI.SS.UserM
using NPOI.HSSF.U
using NPOI.HSSF.UserM
using NPOI.XSSF.UserM
/// &summary&
///NPOI操作Excel,这个例子是针对Excel 2007.&
/// &/summary&
public class ExcelHelper
& & public DataTable ImportExcelFile(string filePath)
& & & & #region//初始化信息
& & & & try
& & & & & & using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
& & & & & & {
& & & & & & & & hssfworkbook = new XSSFWorkbook(file);
& & & & & & }
& & & & catch (Exception e)
& & & & & &
& & & & #endregion
& & & & ISheet sheet = hssfworkbook.GetSheetAt(0);
& & & & System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
& & & & DataTable dt = new DataTable();
& & & & //一行最后一个方格的编号 即总的列数
& & & & for (int j = 0; j & (sheet.GetRow(0).LastCellNum); j++)
& & & & & & dt.Columns.Add(Convert.ToChar(((int)'A') + j).ToString());
& & & & while (rows.MoveNext())
& & & & & & IRow row = (XSSFRow)rows.C
& & & & & & DataRow dr = dt.NewRow();
& & & & & & for (int i = 0; i & row.LastCellN i++)
& & & & & & {
& & & & & & & & ICell cell = row.GetCell(i);
& & & & & & & & if (cell == null)
& & & & & & & & {
& & & & & & & & & & dr[i] =
& & & & & & & & }
& & & & & & & & else
& & & & & & & & {
& & & & & & & & & & dr[i] = cell.ToString();
& & & & & & & & }
& & & & & & }
& & & & & & dt.Rows.Add(dr);
& &&hssfworkbook=
& & sheet =&
绑定GridView显示只需要两名代码
& & this.gvExcel.DataSource =
& & this.gvExcel.DataBind();
3、NPOI中的日期处理,NOPI版本为2.0.2。
POI对单元格日期处理很弱,没有针对的类型,日期类型取出来的也是一个double值,所以同样作为数值类型。
*Excel中的日期格式在解析为DataTable时会依格式不同解析为不同的值,
因为所有的日期格式都可以通过下面DataFormat来判断,
*excel2013,测试结果如下:
*时分(HH:mm) - 20,时间(HH:mm:ss) - 21,日期时间(yyyy-MM-dd HH:mm:ss) - 22,
*年月(yyyy-MM) - 17,年月日(yyyy-MM-dd)-14,
yyyy年m月-------57,月日(MM-dd) - 58,
*yyyy年m月d日---31,h时mm分 &-------32
&if (cell.CellType == CellType.NUMERIC)
& & & short format = cell.CellStyle.DataF
& & & if (format == 14 || format == 31 || format == 57 || format == 58)
& & & & & DateTime date = cell.DateCellV
& & & & & string re = date.ToString(&yyy-MM-dd&);
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致}

我要回帖

更多关于 npoi list导出excel 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信