Winform中通过NPOI导出Excel时通过ICellStyle和IDataFormat格式化日期显示格式
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Winform中通过NPOI导出Excel时通过ICellStyle和IDataFormat格式化日期显示格式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                場景
Winform中通過NPOI導出Excel的三種方式(HSSFWorkbook,XSSFWorkbook,SXSSFWorkbook)附代碼下載:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/106423452
在上面介紹了NPOI的三種導出Excel的方式后,如果想在導出的Excel中添加照片,該怎樣實現。
注:
博客主頁:
https://blog.csdn.net/badao_liumang_qizhi 
 關注公眾號
 霸道的程序猿
 獲取編程相關電子書、教程推送與免費下載。
實現
為了構建導出數據,新建一個類
??? public class DataItem{public int Age { get; set; }public string Name { get; set; }public string Address { get; set; }public int Sex { get; set; }public DateTime Birth { get; set; }}然后接著上面進行導出的邏輯
首先構建一個要導出的數據
??????? List<DataItem> ItemList = new List<DataItem>(){new DataItem() {Name = "霸道",Age = 24,Address = "中國",Sex = 1,Birth = DateTime.Now},new DataItem() {Name = "流氓",Age = 25,Address = "北京",Sex = 0,Birth = DateTime.Now},new DataItem() {Name = "氣質",Age = 26,Address = "上海",Sex = 0,Birth = DateTime.Now},new DataItem() {Name = "程序猿",Age = 27,Address = "青島",Sex = 1,Birth = DateTime.Now},};主要是構建Birth這個屬性
通過上面的博客添加了NPOI的引用后,拖拽一個按鈕,在按鈕的點擊事件中
?private void button5_Click(object sender, EventArgs e){//創建XSSFWorkbook對象XSSFWorkbook wb = new XSSFWorkbook();//創建sheet 并指定sheet的名字gISheet sheet1 = wb.CreateSheet("詳細數據");//新建style對象并設置樣式屬性ICellStyle style1 = wb.CreateCellStyle();//樣式IFont font1 = wb.CreateFont();//字體font1.FontName = "宋體";font1.FontHeightInPoints = 11;font1.Boldweight = (short)FontBoldWeight.Bold;style1.SetFont(font1);//樣式里的字體設置具體的字體樣式ICellStyle dateStyle = wb.CreateCellStyle();//樣式dateStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;//文字水平對齊方式dateStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;//文字垂直對齊方式//設置數據顯示格式IDataFormat dataFormatCustom = wb.CreateDataFormat();dateStyle.SetFont(font1);dateStyle.DataFormat = dataFormatCustom.GetFormat("yyyy-MM-dd HH:mm:ss");//創建第一行IRow row0 = sheet1.CreateRow(0);//創建第一行第一列并設置值row0.CreateCell(0).SetCellValue("姓名");//獲取第一行第一列并設置樣式row0.GetCell(0).CellStyle = style1;row0.CreateCell(1).SetCellValue("年齡");row0.GetCell(1).CellStyle = style1;row0.CreateCell(2).SetCellValue("地址");row0.GetCell(2).CellStyle = style1;row0.CreateCell(3).SetCellValue("性別");row0.GetCell(3).CellStyle = style1;row0.CreateCell(4).SetCellValue("生日");row0.GetCell(4).CellStyle = style1;//循環添加數據foreach (DataItem item in ItemList){int item_index = ItemList.IndexOf(item);//從第二行開始IRow rowi = sheet1.CreateRow(item_index + 1);rowi.CreateCell(0).SetCellValue(item.Name);rowi.CreateCell(1).SetCellValue(item.Age);rowi.CreateCell(2).SetCellValue(item.Address);rowi.CreateCell(3).SetCellValue(item.Sex);rowi.CreateCell(4).SetCellValue(item.Birth);rowi.GetCell(4).CellStyle = dateStyle;//設置列寬度,256*字符數,因為單位是1/256個字符sheet1.SetColumnWidth(item_index, 256 * item.Address.Length * 4);}try{//將內存中的數據寫入磁盤using (FileStream filestream = new FileStream(System.IO.Path.Combine(@"D:\", "liumang.xlsx"), FileMode.Create)){wb.Write(filestream);filestream.Close();}}catch (Exception ex){Console.Write(ex);}MessageBox.Show("導出完成");}主要是通過新建一個CellStyle樣式
??????????? ICellStyle dateStyle = wb.CreateCellStyle();//樣式dateStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;//文字水平對齊方式dateStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;//文字垂直對齊方式//設置數據顯示格式IDataFormat dataFormatCustom = wb.CreateDataFormat();dateStyle.SetFont(font1);dateStyle.DataFormat = dataFormatCustom.GetFormat("yyyy-MM-dd HH:mm:ss");然后在循環添加每一行是通過
rowi.GetCell(4).CellStyle = dateStyle;來設置日期的顯示格式
首先對象屬性里面的時間是這樣顯示的
?
首先測試下如果不加設置CellStyle的這一行代碼
?
如果加上這句設置的代碼后
?
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的Winform中通过NPOI导出Excel时通过ICellStyle和IDataFormat格式化日期显示格式的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: Winforn中通过NPOI导出Exce
 - 下一篇: Winform中导出Excel数据量百万