Nopi Excel下载功能

Nopi  Excel下载功能  



#region //table

            var list = _instrumentservice.GetInstrManageStatisticDto(null,null,null);
            DataTable table = new DataTable();  //1
            table.Columns.Add("管理员姓名", typeof(string));
            table.Columns.Add("所在学院-所在实验室", typeof(string));
            table.Columns.Add("累计预约总时长(本年度)", typeof(string));
            table.Columns.Add("累计预约结算时长(本年度)", typeof(string));

            List<int> widths = new List<int>(); widths.Add(7000); widths.Add(7000); widths.Add(7000); widths.Add(7000);//5
            foreach (var item in list)
            {
                table.Rows.Add(item.PinYin_AdminName + "_" + item.AdminName, item.DepartName + "-" + item.LabName, item.OrderTotalTime + "小时", item.SettledTotalTime + "小时");
            }
            #endregion
            string sheetName = "实验人员工作评价(本年度)"; //3
            string cellTitleValue = sheetName + "---" + DateTime.Now.Year + "-1  至   " + DateTime.Now.Year + "-" + DateTime.Now.Month;//4
            //int columncount = 7;  //5
            #region  //styleTitle
            IWorkbook workbook = new HSSFWorkbook();//2
            ICellStyle styleTitle = workbook.CreateCellStyle();  //6
            IFont font = SetFont(workbook, "BOLD", 14, "Arial");  //设置字体 //workbook.CreateFont();
            //font.Boldweight = (short)FontBoldWeight.BOLD;//加粗
            //font.FontHeightInPoints = 14;
            styleTitle.SetFont(font);
            styleTitle.FillForegroundColor = HSSFColor.WHITE.index;
            styleTitle.FillPattern = FillPatternType.LEAST_DOTS; //背景图文
            styleTitle.FillBackgroundColor = HSSFColor.WHITE.index;  //背景色
            styleTitle = SetCellStyle(styleTitle, HorizontalAlignment.CENTER, VerticalAlignment.CENTER, CellBorderType.THIN, CellBorderType.THIN, CellBorderType.THIN, CellBorderType.THIN, FillPatternType.LEAST_DOTS);
            //styleTitle.Alignment = HorizontalAlignment.CENTER;  //左右 横向样式
            //styleTitle.VerticalAlignment = VerticalAlignment.CENTER;//垂直样式

            //create a font style


            ICellStyle StyleHead = workbook.CreateCellStyle();  //7
            IFont FontCell = SetFont(workbook, "NORMAL", 12, "Arial");  //设置字体
            //IFont FontCell = workbook.CreateFont();//创建字体样式
            //FontCell.Boldweight = (short)FontBoldWeight.NORMAL;//加粗
            //FontCell.FontHeightInPoints = 12; //字体大小
            //FontCell.FontName = "Arial";  //字体
            StyleHead.SetFont(FontCell);  //加入字体样式
            StyleHead.SetFont(FontCell);
            StyleHead.FillForegroundColor = HSSFColor.WHITE.index;
            StyleHead.FillBackgroundColor = HSSFColor.WHITE.index;
            StyleHead = SetCellStyle(StyleHead, HorizontalAlignment.LEFT, VerticalAlignment.CENTER, CellBorderType.THIN, CellBorderType.THIN, CellBorderType.THIN, CellBorderType.THIN, FillPatternType.LEAST_DOTS);
            //StyleHead.FillForegroundColor = HSSFColor.WHITE.index; //
            //StyleHead.FillPattern = FillPatternType.LEAST_DOTS; //
            //StyleHead.FillBackgroundColor = HSSFColor.WHITE.index; //背景色
            //StyleHead.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER;//横样式
            //StyleHead.VerticalAlignment = VerticalAlignment.CENTER;//垂直样式
            //StyleHead.BorderBottom = CellBorderType.THIN; //边框
            //StyleHead.BorderTop = CellBorderType.THIN;//边框
            //StyleHead.BorderLeft = CellBorderType.THIN;//边框
            //StyleHead.BorderRight = CellBorderType.THIN;//边框

            ICellStyle StyleCell = workbook.CreateCellStyle(); //8
            FontCell = SetFont(workbook, "NORMAL", 12, "Arial");  //设置字体
            StyleCell.SetFont(FontCell);
            StyleCell.FillForegroundColor = HSSFColor.WHITE.index;
            StyleCell.FillBackgroundColor = HSSFColor.WHITE.index;
            StyleCell = SetCellStyle(StyleCell, HorizontalAlignment.LEFT, VerticalAlignment.CENTER, CellBorderType.THIN, CellBorderType.THIN, CellBorderType.THIN, CellBorderType.THIN, FillPatternType.LEAST_DOTS);
            //FontCell = workbook.CreateFont();//创建字体样式
            //FontCell.Boldweight = (short)FontBoldWeight.NORMAL;//普通
            //FontCell.FontHeightInPoints = 12;
            //FontCell.FontName = "Arial";
            //StyleCell.SetFont(FontCell);

            //StyleCell.Alignment =HorizontalAlignment.LEFT;//横样式
            //StyleCell.VerticalAlignment = VerticalAlignment.CENTER;//垂直样式

            //StyleCell.BorderBottom = CellBorderType.THIN;
            //StyleCell.BorderTop = CellBorderType.THIN;
            //StyleCell.BorderLeft = CellBorderType.THIN;
            //StyleCell.BorderRight = CellBorderType.THIN;

            //StyleCell.FillForegroundColor = HSSFColor.WHITE.index;
            //StyleCell.FillPattern = FillPatternType.LEAST_DOTS;
            //StyleCell.FillBackgroundColor = HSSFColor.WHITE.index;
            #endregion
            //HttpContext.Request
            DownExcel(table, workbook, sheetName, cellTitleValue, widths, styleTitle, StyleHead, StyleCell, HttpContext);
            return new EmptyResult();#region //table
            var list = _instrumentservice.GetInstrManageStatisticDto(null,null,null);
            DataTable table = new DataTable();  //1
            table.Columns.Add("管理员姓名", typeof(string));
            table.Columns.Add("所在学院-所在实验室", typeof(string));
            table.Columns.Add("累计预约总时长(本年度)", typeof(string));
            table.Columns.Add("累计预约结算时长(本年度)", typeof(string));

            List<int> widths = new List<int>(); widths.Add(7000); widths.Add(7000); widths.Add(7000); widths.Add(7000);//5
            foreach (var item in list)
            {
                table.Rows.Add(item.PinYin_AdminName + "_" + item.AdminName, item.DepartName + "-" + item.LabName, item.OrderTotalTime + "小时", item.SettledTotalTime + "小时");
            }
            #endregion
            string sheetName = "实验人员工作评价(本年度)"; //3
            string cellTitleValue = sheetName + "---" + DateTime.Now.Year + "-1  至   " + DateTime.Now.Year + "-" + DateTime.Now.Month;//4
            //int columncount = 7;  //5
            #region  //styleTitle
            IWorkbook workbook = new HSSFWorkbook();//2
            ICellStyle styleTitle = workbook.CreateCellStyle();  //6
            IFont font = SetFont(workbook, "BOLD", 14, "Arial");  //设置字体 //workbook.CreateFont();
            //font.Boldweight = (short)FontBoldWeight.BOLD;//加粗
            //font.FontHeightInPoints = 14;
            styleTitle.SetFont(font);
            styleTitle.FillForegroundColor = HSSFColor.WHITE.index;
            styleTitle.FillPattern = FillPatternType.LEAST_DOTS; //背景图文
            styleTitle.FillBackgroundColor = HSSFColor.WHITE.index;  //背景色
            styleTitle = SetCellStyle(styleTitle, HorizontalAlignment.CENTER, VerticalAlignment.CENTER, CellBorderType.THIN, CellBorderType.THIN, CellBorderType.THIN, CellBorderType.THIN, FillPatternType.LEAST_DOTS);
            //styleTitle.Alignment = HorizontalAlignment.CENTER;  //左右 横向样式
            //styleTitle.VerticalAlignment = VerticalAlignment.CENTER;//垂直样式

            //create a font style


            ICellStyle StyleHead = workbook.CreateCellStyle();  //7
            IFont FontCell = SetFont(workbook, "NORMAL", 12, "Arial");  //设置字体
            //IFont FontCell = workbook.CreateFont();//创建字体样式
            //FontCell.Boldweight = (short)FontBoldWeight.NORMAL;//加粗
            //FontCell.FontHeightInPoints = 12; //字体大小
            //FontCell.FontName = "Arial";  //字体
            StyleHead.SetFont(FontCell);  //加入字体样式
            StyleHead.SetFont(FontCell);
            StyleHead.FillForegroundColor = HSSFColor.WHITE.index;
            StyleHead.FillBackgroundColor = HSSFColor.WHITE.index;
            StyleHead = SetCellStyle(StyleHead, HorizontalAlignment.LEFT, VerticalAlignment.CENTER, CellBorderType.THIN, CellBorderType.THIN, CellBorderType.THIN, CellBorderType.THIN, FillPatternType.LEAST_DOTS);
            //StyleHead.FillForegroundColor = HSSFColor.WHITE.index; //
            //StyleHead.FillPattern = FillPatternType.LEAST_DOTS; //
            //StyleHead.FillBackgroundColor = HSSFColor.WHITE.index; //背景色
            //StyleHead.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER;//横样式
            //StyleHead.VerticalAlignment = VerticalAlignment.CENTER;//垂直样式
            //StyleHead.BorderBottom = CellBorderType.THIN; //边框
            //StyleHead.BorderTop = CellBorderType.THIN;//边框
            //StyleHead.BorderLeft = CellBorderType.THIN;//边框
            //StyleHead.BorderRight = CellBorderType.THIN;//边框

            ICellStyle StyleCell = workbook.CreateCellStyle(); //8
            FontCell = SetFont(workbook, "NORMAL", 12, "Arial");  //设置字体
            StyleCell.SetFont(FontCell);
            StyleCell.FillForegroundColor = HSSFColor.WHITE.index;
            StyleCell.FillBackgroundColor = HSSFColor.WHITE.index;
            StyleCell = SetCellStyle(StyleCell, HorizontalAlignment.LEFT, VerticalAlignment.CENTER, CellBorderType.THIN, CellBorderType.THIN, CellBorderType.THIN, CellBorderType.THIN, FillPatternType.LEAST_DOTS);
            //FontCell = workbook.CreateFont();//创建字体样式
            //FontCell.Boldweight = (short)FontBoldWeight.NORMAL;//普通
            //FontCell.FontHeightInPoints = 12;
            //FontCell.FontName = "Arial";
            //StyleCell.SetFont(FontCell);

            //StyleCell.Alignment =HorizontalAlignment.LEFT;//横样式
            //StyleCell.VerticalAlignment = VerticalAlignment.CENTER;//垂直样式

            //StyleCell.BorderBottom = CellBorderType.THIN;
            //StyleCell.BorderTop = CellBorderType.THIN;
            //StyleCell.BorderLeft = CellBorderType.THIN;
            //StyleCell.BorderRight = CellBorderType.THIN;

            //StyleCell.FillForegroundColor = HSSFColor.WHITE.index;
            //StyleCell.FillPattern = FillPatternType.LEAST_DOTS;
            //StyleCell.FillBackgroundColor = HSSFColor.WHITE.index;
            #endregion
            //HttpContext.Request
            DownExcel(table, workbook, sheetName, cellTitleValue, widths, styleTitle, StyleHead, StyleCell, HttpContext);
            return new EmptyResult();
引用之后,使用该方法读取excel文件 #region 读取Excel public static DataTable ImportExcel(string filePath) { try { //根据路径通过已存在的excel来创建HSSFWorkbook,即整个excel文档 FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); //判断文件后缀名是xls,还是xlsx,如果是xls,使用HSSFWorkbook;如果是xlsx,使用XSSFWorkbook XSSFWorkbook workbook = new XSSFWorkbook(file); //获取excel的第一个sheet ISheet sheet = workbook.GetSheetAt(0); DataTable table = new DataTable(); //获取sheet的首行 IRow headerRow = sheet.GetRow(0); if (headerRow == null) headerRow = sheet.GetRow(1); //一行最后一个方格的编号 即总的列数 int cellCount = headerRow.LastCellNum; for (int i = headerRow.FirstCellNum; i < cellCount; i++) { ICell cell = headerRow.GetCell(i); if (cell != null) { DataColumn column = new DataColumn(cell.StringCellValue); table.Columns.Add(column); } } //最后一列的标号 即总的行数 // int rowCount = sheet.LastRowNum; cellCount = table.Columns.Count; for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++) { IRow row = sheet.GetRow(i); if (row == null) continue; DataRow dataRow = table.NewRow(); bool isAdd = false; for (int j = row.FirstCellNum; j < cellCount; j++) { if (row.GetCell(j) == null) continue; //row.GetCell(j).SetCellType(CellType.STRING); if (row.GetCell(j) != null) { if (row.GetCell(j).CellType == CellType.NUMERIC) dataRow[j] = (int)(row.GetCell(j).NumericCellValue); else dataRow[j] = row.GetCell(j).StringCellValue; isAdd = true; } } if (isAdd) table.Rows.Add(dataRow); } workbook = null; sheet = null; return table; } catch (Exception ex) { MessageBox.Show(ex.ToString()); return null; } } #endregion
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值