網(wǎng)站首頁 編程語言 正文
本文為大家分享了基于NPOI用C#開發(fā)的Excel以及表格設(shè)置,供大家參考,具體內(nèi)容如下
最近在項(xiàng)目中需要導(dǎo)出Excel。在這里做個(gè)記錄。在網(wǎng)上查閱了一些資料。將自己最終的代碼分享在這里,以供自己日后查閱,如果能給陌生的你帶來方便,那便更好。
開發(fā)的過程中也遇到了一個(gè)問題,設(shè)置字體會(huì)導(dǎo)致打開Excel時(shí)報(bào)錯(cuò)(錯(cuò)誤:此文件中的某些文本格式可能已經(jīng)更改,因?yàn)樗呀?jīng)超出最多允許的字體數(shù)),并且設(shè)置失敗。這個(gè)問題產(chǎn)生的原因是因?yàn)轭l繁的創(chuàng)建字體,這個(gè)在我參考的代碼中是有問題,我做了些改善。如果你有更優(yōu)的方案,期待向你學(xué)習(xí)。
public class ExcelDAL ? ? { ? ? ? ? #region 定義單元格常用到樣式的枚舉 ? ? ? ? public enum stylexls ? ? ? ? { ? ? ? ? ? ? 頭, ? ? ? ? ? ? 列標(biāo)題, ? ? ? ? ? ? url, ? ? ? ? ? ? 時(shí)間, ? ? ? ? ? ? 數(shù)字, ? ? ? ? ? ? 錢, ? ? ? ? ? ? 百分比, ? ? ? ? ? ? 中文大寫, ? ? ? ? ? ? 科學(xué)計(jì)數(shù)法, ? ? ? ? ? ? 默認(rèn) ? ? ? ? } ? ? ? ? #endregion ? ? ? ? //定義工作薄 ? ? ? ? private static IWorkbook m_workbook;? ? ? ? ? //定義sheet表 ? ? ? ? private static ISheet m_sheet; ? ? ? ? //表名 ? ? ? ? private static Listm_sheets=new List (); ? ? ? ? private static ICellStyle m_cellStyle; ? ? ? ? private static IDataFormat m_datastyle; ? ? ? ? //字體 ? ? ? ? private static IFont m_font20; ? ? ? ? //字體 ? ? ? ? private static IFont m_font12; ? ? ? ? //字體 ? ? ? ? private static IFont m_font; ? ? ? ? /// ? ? ? ? /// 創(chuàng)建Excel表 ? ? ? ? /// ? ? ? ? /// 傳遞datatable數(shù)據(jù)類型 ? ? ? ? /// 文件保存路徑 ? ? ? ? /// 工作表名 ? ? ? ? /// 表格標(biāo)題名 ? ? ? ? ///? ? ? ? public static bool ExportExcel(System.Data.DataTable dt, string filePath, string sheetName,string headerName="考勤表") ? ? ? ? { ? ? ? ? ? ? ICellStyle cellstytle = null; ? ? ? ? ? ? try ? ? ? ? ? ? { ? ? ? ? ? ? ? ? //如果Excel存在就獲取IWorkbook對象,否則就重新創(chuàng)建 ? ? ? ? ? ? ? ? if (File.Exists(filePath)) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); ? ? ? ? ? ? ? ? ? ? if (filePath.IndexOf(".xlsx") > 0) // 2007版本 ? ? ? ? ? ? ? ? ? ? ? ? m_workbook = new XSSFWorkbook(fs); ? ? ? ? ? ? ? ? ? ? else if (filePath.IndexOf(".xls") > 0) // 2003版本 ? ? ? ? ? ? ? ? ? ? ? ? m_workbook = new HSSFWorkbook(fs); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? //創(chuàng)建一個(gè)工作簿 ? ? ? ? ? ? ? ? ? ? m_workbook = new HSSFWorkbook(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? if (m_workbook != null) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? //獲取所有SheetName ? ? ? ? ? ? ? ? ? ? int count = m_workbook.NumberOfSheets; ? ? ? ? ? ? ? ? ? ? //如果該工作簿不存在表就創(chuàng)建新表 ? ? ? ? ? ? ? ? ? ? if (count < 1) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? //創(chuàng)建一個(gè) sheet 表 ? ? ? ? ? ? ? ? ? ? ? ? m_sheet = m_workbook.CreateSheet(sheetName); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? m_sheets.Clear(); ? ? ? ? ? ? ? ? ? ? ? ? for (int i = 0; i < count; i++) ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? m_sheet = m_workbook.GetSheetAt(i); ? ? ? ? ? ? ? ? ? ? ? ? ? ? m_sheets.Add(m_sheet.SheetName); ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? if(m_sheets.Contains(sheetName)) ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? m_sheet = m_workbook.CreateSheet(sheetName+System.DateTime.Now.ToString("HH-mm-ss")+"副本"); ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? m_sheet = m_workbook.CreateSheet(sheetName); ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? return false; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? #region 打印設(shè)置 ? ? ? ? ? ? ? ? m_sheet.PrintSetup.Copies = 3; ? ? ? ? ? ? ? ? m_sheet.PrintSetup.Landscape = false; ? ? ? ? ? ? ? ? m_sheet.PrintSetup.Scale = 100; ? ? ? ? ? ? ? ? //紙張?jiān)O(shè)置,A4紙 ? ? ? ? ? ? ? ? m_sheet.PrintSetup.PaperSize = 9; ? ? ? ? ? ? ? ? //打印網(wǎng)格線 ? ? ? ? ? ? ? ? m_sheet.IsPrintGridlines = true; ? ? ? ? ? ? ? ? #endregion ? ? ? ? ? ? ? ? ? #region 設(shè)置表頭 ? ? ? ? ? ? ? ? m_sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, dt.Columns.Count-1)); //合并單元格 ? ? ? ? ? ? ? ? IRow row0 = m_sheet.CreateRow(0); ?//創(chuàng)建一行 ? ? ? ? ? ? ? ? row0.Height = 50 * 20; ? ? ? ? ? ? ? ? ICell icelltop0 = row0.CreateCell(0); ?//創(chuàng)建一個(gè)單元格 ? ? ? ? ? ? ? ? IFont font = m_workbook.CreateFont(); ? ? ? ? ? ? ? ? font.FontHeightInPoints = 30; ? ? ? ? ? ? ? ? icelltop0.CellStyle = Getcellstyle(m_workbook, stylexls.頭); ? ? ? ? ? ? ? ? icelltop0.SetCellValue(headerName); ? ? ? ? ? ? ? ? #endregion ? ? ? ? ? ? ? ? ? #region 設(shè)置列 ? ? ? ? ? ? ? ? IRow rowH = m_sheet.CreateRow(1); ? ? ? ? ? ? ? ? cellstytle= Getcellstyle(m_workbook, stylexls.列標(biāo)題); ? ? ? ? ? ? ? ? //設(shè)置列名 ? ? ? ? ? ? ? ? foreach (DataColumn col in dt.Columns) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? //創(chuàng)建單元格并設(shè)置單元格內(nèi)容 ? ? ? ? ? ? ? ? ? ? rowH.CreateCell(col.Ordinal).SetCellValue(col.Caption); ? ? ? ? ? ? ? ? ? ? ? //設(shè)置單元格格式 ? ? ? ? ? ? ? ? ? ? rowH.Cells[col.Ordinal].CellStyle = cellstytle; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? #endregion ? ? ? ? ? ? ? ? ? //寫入數(shù)據(jù) ? ? ? ? ? ? ? ? cellstytle = Getcellstyle(m_workbook, stylexls.默認(rèn)); ? ? ? ? ? ? ? ? for (int i = 0; i < dt.Rows.Count; i++) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? //跳過前兩行,第一行為標(biāo)題,第二行為列名? ? ? ? ? ? ? ? ? ? ? IRow row = m_sheet.CreateRow(i + 2); ? ? ? ? ? ? ? ? ? ? ICell cell = row.CreateCell(0); ? ? ? ? ? ? ? ? ? ? for (int j = 0; j < dt.Columns.Count; j++) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? cell = row.CreateCell(j); ? ? ? ? ? ? ? ? ? ? ? ? cell.SetCellValue(dt.Rows[i][j].ToString()); ? ? ? ? ? ? ? ? ? ? ? ? cell.CellStyle = cellstytle; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? //獲取當(dāng)前列的寬度,然后對比本列的長度,取最大值 ? ? ? ? ? ? ? ? for (int columnNum = 0; columnNum <= dt.Rows.Count; columnNum++) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? int columnWidth = m_sheet.GetColumnWidth(columnNum) / 256; ? ? ? ? ? ? ? ? ? ? for (int rowNum = 1; rowNum <= m_sheet.LastRowNum; rowNum++) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? IRow currentRow; ? ? ? ? ? ? ? ? ? ? ? ? //當(dāng)前行未被使用過 ? ? ? ? ? ? ? ? ? ? ? ? if (m_sheet.GetRow(rowNum) == null) ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? currentRow = m_sheet.CreateRow(rowNum); ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? currentRow = m_sheet.GetRow(rowNum); ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? if (currentRow.GetCell(columnNum) != null) ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ICell currentCell = currentRow.GetCell(columnNum); ? ? ? ? ? ? ? ? ? ? ? ? ? ? int length = Encoding.Default.GetBytes(currentCell.ToString()).Length; ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (columnWidth < length) ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? columnWidth = length + 10; ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? m_sheet.SetColumnWidth(columnNum, columnWidth * 256); ? ? ? ? ? ? ? ? ? ? //m_sheet.SetColumnWidth(0, 30 * 256); ? ? ? ? ? ? ? ? ? ? //m_sheet.SetColumnWidth(1, 10 * 256); ? ? ? ? ? ? ? ? ? ? //m_sheet.SetColumnWidth(2, 25 * 256); ? ? ? ? ? ? ? ? ? ? //m_sheet.SetColumnWidth(3, 25 * 256); ? ? ? ? ? ? ? ? ? ? //m_sheet.SetColumnWidth(4, 10 * 256); ? ? ? ? ? ? ? ? ? ? //m_sheet.SetColumnWidth(5, 10 * 256); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? //創(chuàng)建文件 ? ? ? ? ? ? ? ? FileStream file = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write); ? ? ? ? ? ? ? ? ? //創(chuàng)建一個(gè) IO 流 ? ? ? ? ? ? ? ? MemoryStream ms = new MemoryStream(); ? ? ? ? ? ? ? ? ? //寫入到流 ? ? ? ? ? ? ? ? m_workbook.Write(ms); ? ? ? ? ? ? ? ? ? //轉(zhuǎn)換為字節(jié)數(shù)組 ? ? ? ? ? ? ? ? byte[] bytes = ms.ToArray(); ? ? ? ? ? ? ? ? ? file.Write(bytes, 0, bytes.Length); ? ? ? ? ? ? ? ? file.Flush(); ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? //釋放資源 ? ? ? ? ? ? ? ? bytes = null; ? ? ? ? ? ? ? ? ? ms.Close(); ? ? ? ? ? ? ? ? ms.Dispose(); ? ? ? ? ? ? ? ? ? file.Close(); ? ? ? ? ? ? ? ? file.Dispose(); ? ? ? ? ? ? ? ? ? m_workbook.Close(); ? ? ? ? ? ? ? ? m_sheet = null; ? ? ? ? ? ? ? ? m_workbook = null; ? ? ? ? ? ? ? ? m_cellStyle = null; ? ? ? ? ? ? ? ? m_datastyle = null; ? ? ? ? ? ? ? ? m_font = null; ? ? ? ? ? ? ? ? m_font12 = null; ? ? ? ? ? ? ? ? m_font20 = null; ? ? ? ? ? ? ? ? return true; ? ? ? ? ? ? } ? ? ? ? ? ? catch (Exception ex) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? return false; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? #region 定義單元格常用到樣式 ? ? ? ? static ICellStyle Getcellstyle(IWorkbook wb, stylexls str) ? ? ? ? { ? ? ? ? ? ? try ? ? ? ? ? ? { ? ? ? ? ? ? ? ? //CreateFont()不能頻繁創(chuàng)建,會(huì)導(dǎo)致打開EXCEL表的時(shí)候報(bào)如下錯(cuò)誤: ? ? ? ? ? ? ? ? //此文件中的某些文本格式可能已經(jīng)更改,因?yàn)樗呀?jīng)超出最多允許的字體數(shù)。 ? ? ? ? ? ? ? ? if (m_font20 == null) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? m_font20 = wb.CreateFont(); ? ? ? ? ? ? ? ? ? ? m_font20.FontHeightInPoints = 20; ? ? ? ? ? ? ? ? ? ? m_font20.FontName = "微軟雅黑"; ? ? ? ? ? ? ? ? ? ? m_font20.Boldweight = (short)FontBoldWeight.Bold; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? if (m_font12 == null) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? m_font12 = wb.CreateFont(); ? ? ? ? ? ? ? ? ? ? m_font12.FontHeightInPoints = 12; ? ? ? ? ? ? ? ? ? ? m_font12.FontName = "微軟雅黑"; ? ? ? ? ? ? ? ? ? ? m_font12.Boldweight = (short)FontBoldWeight.Bold; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? if (m_font == null) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? m_font = wb.CreateFont(); ? ? ? ? ? ? ? ? ? ? m_font.FontName = "微軟雅黑"; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? //if (m_cellStyle == null) ? ? ? ? ? ? ? ? //{ ? ? ? ? ? ? ? ? ? ? m_cellStyle = wb.CreateCellStyle(); ? ? ? ? ? ? ? ? ? ? //邊框 ? ? ? ? ? ? ? ? ? ? ? m_cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Medium; ? ? ? ? ? ? ? ? ? ? m_cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Medium; ? ? ? ? ? ? ? ? ? ? m_cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Medium; ? ? ? ? ? ? ? ? ? ? m_cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Medium; ? ? ? ? ? ? ? ? ? ? //邊框顏色 ? ? ? ? ? ? ? ? ? ? ? m_cellStyle.BottomBorderColor = HSSFColor.OliveGreen.Blue.Index; ? ? ? ? ? ? ? ? ? ? m_cellStyle.TopBorderColor = HSSFColor.OliveGreen.Blue.Index; ? ? ? ? ? ? ? ? ? ? ? //背景圖形 ? ? ? ? ? ? ? ? ? ? //cellStyle.FillBackgroundColor = HSSFColor.OLIVE_GREEN.BLUE.index; ? ? ? ? ? ? ? ? ? ? ? //cellStyle.FillForegroundColor = HSSFColor.OLIVE_GREEN.BLUE.index; ? ? ? ? ? ? ? ? ? ? ? m_cellStyle.FillForegroundColor = HSSFColor.White.Index; ? ? ? ? ? ? ? ? ? ? // cellStyle.FillPattern = FillPatternType.NO_FILL; ? ? ? ? ? ? ? ? ? ? ? m_cellStyle.FillBackgroundColor = HSSFColor.Blue.Index; ? ? ? ? ? ? ? ? ? ? ? //水平對齊 ? ? ? ? ? ? ? ? ? ? ? m_cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; ? ? ? ? ? ? ? ? ? ? ? //垂直對齊 ? ? ? ? ? ? ? ? ? ? ? m_cellStyle.VerticalAlignment = VerticalAlignment.Center; ? ? ? ? ? ? ? ? ? ? ? //自動(dòng)換行 ? ? ? ? ? ? ? ? ? ? ? m_cellStyle.WrapText = false; ? ? ? ? ? ? ? ? ? ? ? //縮進(jìn) ? ? ? ? ? ? ? ? ? ? //cellStyle.Indention = 0; ? ? ? ? ? ? ? ? //} ? ? ? ? ? ? ? ? //創(chuàng)建格式 ? ? ? ? ? ? ? ? if (m_datastyle == null) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? m_datastyle = wb.CreateDataFormat(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? //上面基本都是設(shè)共公的設(shè)置 ? ? ? ? ? ? ? ? ? //下面列出了常用的字段類型 ? ? ? ? ? ? ? ? ? switch (str) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? case stylexls.頭: ? ? ? ? ? ? ? ? ? ? ? ? //cellStyle.FillPattern = FillPatternType.LEAST_DOTS; ? ? ? ? ? ? ? ? ? ? ? ? ? //設(shè)置為文本格式,也可以為 text,即 dataFormat.GetFormat("text"); ? ? ? ? ? ? ? ? ? ? ? ? m_cellStyle.DataFormat = m_datastyle.GetFormat("@"); ? ? ? ? ? ? ? ? ? ? ? ? m_cellStyle.SetFont(m_font20); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? case stylexls.列標(biāo)題: ? ? ? ? ? ? ? ? ? ? ? ? // cellStyle.FillPattern = FillPatternType.LEAST_DOTS; ? ? ? ? ? ? ? ? ? ? ? ? m_cellStyle.DataFormat = m_datastyle.GetFormat("@"); ? ? ? ? ? ? ? ? ? ? ? ? m_cellStyle.SetFont(m_font12); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? case stylexls.時(shí)間: ? ? ? ? ? ? ? ? ? ? ? ? m_cellStyle.DataFormat = m_datastyle.GetFormat("yyyy/mm/dd"); ? ? ? ? ? ? ? ? ? ? ? ? m_cellStyle.SetFont(m_font); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? case stylexls.數(shù)字: ? ? ? ? ? ? ? ? ? ? ? ? m_cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00"); ? ? ? ? ? ? ? ? ? ? ? ? m_cellStyle.SetFont(m_font); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? case stylexls.錢: ? ? ? ? ? ? ? ? ? ? ? ? m_cellStyle.DataFormat = m_datastyle.GetFormat("¥#,##0"); ? ? ? ? ? ? ? ? ? ? ? ? m_cellStyle.SetFont(m_font); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? case stylexls.url: ? ? ? ? ? ? ? ? ? ? ? ? //IFont fontcolorblue = wb.CreateFont(); ? ? ? ? ? ? ? ? ? ? ? ? //fontcolorblue.Color = HSSFColor.OliveGreen.Blue.Index; ? ? ? ? ? ? ? ? ? ? ? ? //fontcolorblue.IsItalic = true;//下劃線 ? ? ? ? ? ? ? ? ? ? ? ? ? fontcolorblue.Underline = 1; ? ? ? ? ? ? ? ? ? ? ? ? //fontcolorblue.FontName = "微軟雅黑"; ? ? ? ? ? ? ? ? ? ? ? ? //m_cellStyle.SetFont(fontcolorblue); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? case stylexls.百分比: ? ? ? ? ? ? ? ? ? ? ? ? m_cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%"); ? ? ? ? ? ? ? ? ? ? ? ? m_cellStyle.SetFont(m_font); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? case stylexls.中文大寫: ? ? ? ? ? ? ? ? ? ? ? ? m_cellStyle.DataFormat = m_datastyle.GetFormat("[DbNum2][$-804]0"); ? ? ? ? ? ? ? ? ? ? ? ? m_cellStyle.SetFont(m_font); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? case stylexls.科學(xué)計(jì)數(shù)法: ? ? ? ? ? ? ? ? ? ? ? ? m_cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00E+00"); ? ? ? ? ? ? ? ? ? ? ? ? m_cellStyle.SetFont(m_font); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? case stylexls.默認(rèn): ? ? ? ? ? ? ? ? ? ? ? ? m_cellStyle.SetFont(m_font); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? return m_cellStyle; ? ? ? ? ? ? } ? ? ? ? ? ? catch ? ? ? ? ? ? { ? ? ? ? ? ? ? ? return m_cellStyle; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? #endregion ? ? ? }
創(chuàng)建的表格的效果如圖所示:
原文鏈接:https://blog.csdn.net/ezreal_pan/article/details/81205498
相關(guān)推薦
- 2022-06-28 python神經(jīng)網(wǎng)絡(luò)使用Keras構(gòu)建RNN訓(xùn)練_python
- 2022-02-11 SQL中ISNULL函數(shù)使用介紹_數(shù)據(jù)庫其它
- 2023-06-21 Rust?Atomics?and?Locks并發(fā)基礎(chǔ)理解_Rust語言
- 2023-01-29 HTTP與HTTPS超文本傳輸協(xié)議的區(qū)別是什么_經(jīng)驗(yàn)交流
- 2023-01-03 C#短消息提示窗口位置及窗口大小詳解_C#教程
- 2023-10-15 達(dá)夢錯(cuò)誤碼信息-PRO*C 錯(cuò)誤碼匯編
- 2022-02-19 使用.Net?Core實(shí)現(xiàn)的一個(gè)圖形驗(yàn)證碼功能_實(shí)用技巧
- 2022-04-05 MAC中執(zhí)行.sh腳本,/bin/sh^M: bad interpreter:解決辦法
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支