Excel 导入导出类
来源:互联网 发布:淘宝禁止出售药方 编辑:程序博客网 时间:2024/06/10 16:55
#region 导出到Excel /// <summary> /// 导出到Excel /// </summary> /// <param name="table">数据源</param> /// <param name="title">标题</param> /// <returns></returns> public static System.IO.MemoryStream ToExcel(DataTable table, string title) { IWorkbook workBook = new HSSFWorkbook(); ISheet sheet = workBook.CreateSheet("sheet1"); //处理表格标题 IRow row = sheet.CreateRow(0); row.CreateCell(0).SetCellValue(title); sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, table.Columns.Count - 1)); row.Height = 500; ICellStyle cellStyle = workBook.CreateCellStyle(); IFont font = workBook.CreateFont(); font.FontName = "微软雅黑"; font.FontHeightInPoints = 17; cellStyle.SetFont(font); cellStyle.VerticalAlignment = VerticalAlignment.Center; cellStyle.Alignment = HorizontalAlignment.Center; row.Cells[0].CellStyle = cellStyle; //处理表格列头 row = sheet.CreateRow(1); for (int i = 0; i < table.Columns.Count; i++) { var curr = row.CreateCell(i); curr.SetCellValue(table.Columns[i].ColumnName); curr.CellStyle.VerticalAlignment = VerticalAlignment.Center; curr.CellStyle.Alignment = HorizontalAlignment.Center; row.Height = 350; sheet.AutoSizeColumn(i); } //处理数据内容 for (int i = 0; i < table.Rows.Count; i++) { row = sheet.CreateRow(2 + i); row.Height = 250; for (int j = 0; j < table.Columns.Count; j++) { row.CreateCell(j).SetCellValue(table.Rows[i][j].ToString()); sheet.SetColumnWidth(j, 256 * 15); } } System.IO.MemoryStream ms = new System.IO.MemoryStream(); workBook.Write(ms); ms.Seek(0, SeekOrigin.Begin); return ms; } #endregion
#region Oledb方式读取EXCEL /// <summary> /// Oledb方式读取EXCEL /// </summary> /// <param name="fileNamePath">文件路径</param> /// <returns></returns> private DataTable ReadExcelByOledb(string fileNamePath) { string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;data source=" + fileNamePath; //只读第一个表 OleDbConnection oledbconn1 = new OleDbConnection(connStr); oledbconn1.Open(); DataTable _table = oledbconn1.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" }); string strTableName = string.Empty; if (_table.Rows.Count > 0) { strTableName = _table.Rows[0]["TABLE_NAME"].ToString().Trim(); string sql = string.Format("SELECT * FROM [{0}]", strTableName); _table = new DataTable(); OleDbDataAdapter da = new OleDbDataAdapter(sql, oledbconn1); da.Fill(_table); } oledbconn1.Close(); return _table; } #endregion
0 0
- EXCEL导入导出类
- Excel 导入导出类
- Excel导入导出工具类
- excel导入导出工具类
- Excel导入导出工具类
- java导出excel,导入excel,导出csv工具类整理
- C#导入导出Excel通用类(SamWang)
- 使用NPOI 导入导出Excel的类
- excel 导入导出 poi工具类
- 导入导出Excel工具类ExcelUtil
- 导入导出Excel工具类ExcelUtil
- Excel + SQL Server 导入导出类
- C# WINFORM Excel 导入导出 类
- 导入导出Excel工具类ExcelUtil
- C#导入导出EXCEL文件的类
- 通用的excel导入导出类
- java 导入导出Excel工具类ExcelUtil
- excel导入导出通用工具类
- 定时设置(界面适配器list、文件数据载入,校验,场景设置manager、xml的解析)
- laravel+vue2.0终于搞定了!
- quick-lua与java相互调用出现崩溃
- CXF发布restful WebService的入门例子(服务器端)
- 课程设计 电子投票系统
- Excel 导入导出类
- java https 导入cer证书
- 索引数组、关联数组和静态数组、动态数组
- win764位Oracle监听六分钟连通处理
- 阿里RocketMQ
- iOS MVC模式
- js常用代码块
- 实现两个整数交换的宏程序
- python安装扩展”unable to find vcvarsall.bat“的解决办法