使用第三方控件NPOI操作excel
来源:互联网 发布:做淘宝客服能学到什么 编辑:程序博客网 时间:2024/05/16 17:50
/// <summary>创建xls文件 /// </summary> public static void CreateXLS() { //创建工作簿 HSSFWorkbook wk = new HSSFWorkbook(); //创建一个名称为mySheet的表 ISheet tb = wk.CreateSheet("mySheet"); //创建一行,此行为第二行 IRow row = tb.CreateRow(1); for (int i = 0; i < 20; i++) { ICell cell = row.CreateCell(i);//在第二行中创建单元格 cell.SetCellValue(i);//循环往第二行的单元格中添加数据 } using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate)) { wk.Write(fs); Console.Write("文件保存成功!"); } }
/// <summary>读取xls文件 /// </summary> public static void ReadXLS() { StringBuilder sbr = new StringBuilder(); using (FileStream fs = new FileStream(filePath, FileMode.Open))//打开myxls.xls文件 { HSSFWorkbook wk = new HSSFWorkbook(fs);//把xls文件中的数据写入wk中 for (int i = 0; i < wk.NumberOfSheets; i++)//NumberOfSheets是myxls.xls中总共的表述 { ISheet sheet = wk.GetSheetAt(i);//读取当前表数据 for (int j = 0; j <= sheet.LastRowNum; j++)//LastRowNum 是当前表的总行数 { IRow row = sheet.GetRow(j);//读取当前行数据 if (row != null) { sbr.Append("--------------------------------");//读取行与行之间的提示接线 for (int k = 0; k <= row.LastCellNum; k++) { ICell cell = row.GetCell(k);//当前单元格 if (cell != null) { sbr.Append(cell.ToString());//获取表格中的数据并转换为字符串类型 } } } } } } //using(StreamWriter wr = new StreamWriter(new FileStream(@"C:\\mytest.txt",FileMode.Append))) using (StreamWriter wr = new StreamWriter(@"C:\\mytest.txt")) { wr.Write(sbr.ToString()); wr.Flush(); } }
public static void xlsUse() { IWorkbook wb = new HSSFWorkbook(); //创建表 ISheet sh = wb.CreateSheet("wsf"); //设置单元格的宽度 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 icelltop0 = row0.CreateCell(0); icelltop0.CellStyle = getCellStyle(wb, styleXls.头); icelltop0.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("百度收录"); using (FileStream stm = File.OpenWrite(@"C:/myMergeCell.xls")) { wb.Write(stm); Console.WriteLine("创建成功!"); } #endregion } #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.index; fontcolorblue.IsItalic = true;//下划线 fontcolorblue.FontName = "微软雅黑"; //边框样式 cellStyle.BorderBottom = BorderStyle.DOTTED; cellStyle.BorderLeft = BorderStyle.HAIR; cellStyle.BorderRight = BorderStyle.HAIR; cellStyle.BorderTop = BorderStyle.DOTTED; //边框颜色 cellStyle.BottomBorderColor = HSSFColor.OLIVE_GREEN.BLUE.index; cellStyle.TopBorderColor = HSSFColor.OLIVE_GREEN.BLUE.index; //背景图形 //cellStyle.FillBackgroundColor = HSSFColor.OLIVE_GREEN.BLUE.index; //cellStyle.FillForegroundColor = HSSFColor.OLIVE_GREEN.BLUE.index; cellStyle.FillForegroundColor = HSSFColor.WHITE.index; //cellStyle.FillPattern = FillPatternType.NO_FILL; cellStyle.FillBackgroundColor = HSSFColor.BLUE.index; //水平对齐 cellStyle.Alignment = HorizontalAlignment.LEFT; //垂直对齐 cellStyle.VerticalAlignment = VerticalAlignment.CENTER; //自动换行 cellStyle.WrapText = true; //缩进;当设置为1时,前面留的空白太大了。希望官网改进,或者是我设置的不对 cellStyle.Indention = 0; //上面基本都是公共的设置 //下面列出了常用的字段类型 switch (str) { case styleXls.头: cellStyle.SetFont(font12); break; case styleXls.时间: IDataFormat datastyle = wb.CreateDataFormat(); cellStyle.DataFormat = datastyle.GetFormat("yyyy/mm/dd"); cellStyle.SetFont(font); break; case styleXls.数字: cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00"); cellStyle.SetFont(font); break; case styleXls.钱: IDataFormat format = wb.CreateDataFormat(); cellStyle.DataFormat = format.GetFormat("¥#,##0"); cellStyle.SetFont(font); break; case styleXls.url: fontcolorblue.Underline = 1; cellStyle.SetFont(fontcolorblue); break; case styleXls.百分比: cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%"); cellStyle.SetFont(font); break; case styleXls.中文大写: IDataFormat format1 = wb.CreateDataFormat(); cellStyle.DataFormat = format1.GetFormat("[DbNum2][$-804]0"); cellStyle.SetFont(font); break; case styleXls.科学计数法: cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00E+00"); cellStyle.SetFont(font); break; case styleXls.默认: cellStyle.SetFont(font); break; } return cellStyle; } #endregion
源自:http://wshoufeng1989.blog.163.com/blog/static/202047033201327112530440/
- 使用第三方控件NPOI操作excel
- .net NPOI 第三方Excel操作
- 在Winform中使用NPOI第三方组件导出Excel
- 使用NPOI操作excel
- 使用第三方.Net NPOI实现.net mvc使用FileResult导出excel(下载到客户端)
- 使用第三方组件NPOI和iTextSharp根据DataTable导出Excel、Word、Pdf
- NPOI 2.1.1 binary-第三方控件读取execl
- 第三方控件使用
- 第三方库操作excel
- 使用NPOI操作Excel导入导出数据
- C#中使用NPOI操作excel
- C# 使用NPOI操作Excel文件
- C#使用NPOI操作Excel错误解决
- .Net操作excel(使用NPOI)
- 使用 npoi 操作excel ”npoi“可从“http://www.codeplex.com/npoi”下载
- Excel操作——使用Com操作和NPOI操作
- NPOI 进行EXCEL操作
- 用NPOI操作Excel
- Java学习资料(三)——API文档
- 你觉得自己牛逼在哪儿?
- dos命令禁用和启用网络链接
- 白春杰学C#OPC[一]
- sed和awk最佳入门教程
- 使用第三方控件NPOI操作excel
- android使用C/C++调用SO库
- 转:教你如何查看端口的使用情况(哪些程序使用了哪些端口)
- ps命令复习
- linux文件与目录管理
- CUDA, 用于大量数据的超级运算:第三节
- 使用lsusb命令查看系统中的USB设备
- 使用TabHost实现卡片选项菜单实例
- Python解决codeforces ---- 5