1 //使用NPOI操做Excel 2 private void ExcelNPOI(System.Data.DataTable dt, HttpContext context) 3 { 4 IWorkbook workbook = null;//工做薄 5 IRow row = null;//行 6 ICell cell = null;//單元格 7 ISheet sheet = null;//工做表 8 try 9 { 10 //若是表中查詢的有數據 11 if (dt != null && dt.Rows.Count > 0) 12 { 13 //建立工做薄 14 //workbook = new HSSFWorkbook(); //導出後綴爲xls 15 workbook = new XSSFWorkbook();//導出後綴爲xlsx 16 sheet = workbook.CreateSheet("Sheet1");//建立一個名稱爲Sheet1的表 17 int rowCount = dt.Rows.Count;//行數 18 int columnCount = dt.Columns.Count;//列數 19 20 //npoi設置Excel樣式 21 ICellStyle cellStyle = workbook.CreateCellStyle(); 22 //設置單元格爲數字格式 23 cellStyle.DataFormat = workbook.CreateDataFormat().GetFormat("0.00"); 24 //居中對齊 25 cellStyle.Alignment = HorizontalAlignment.Center; 26 cellStyle.VerticalAlignment = VerticalAlignment.Center; 27 //邊框 28 cellStyle.BorderTop = BorderStyle.Thin; 29 cellStyle.BorderBottom = BorderStyle.Thin; 30 cellStyle.BorderLeft = BorderStyle.Thin; 31 cellStyle.BorderRight = BorderStyle.Thin; 32 //建立一個字體樣式對象 33 NPOI.SS.UserModel.IFont FontRow = workbook.CreateFont(); 34 //設置字體樣式 35 FontRow.FontName = "宋體"; 36 //設置字體加粗樣式 37 FontRow.Boldweight = (short)FontBoldWeight.Bold; 38 //設置字體大小 39 FontRow.FontHeightInPoints = 12; 40 //是否加粗 41 //FontRow.IsBold = false; 42 //字體樣式添加進去 43 cellStyle.SetFont(FontRow); 44 45 //合併單元格 起始行號,終止行號, 起始列號,終止列號 execl的行列都是從0開始,而不是從1開始 46 sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 5)); 47 48 //添加第一行 並賦值 49 row = sheet.CreateRow(0); 50 row.CreateCell(0).SetCellValue("值"); 51 cell = row.GetCell(0); 52 cell.CellStyle = cellStyle; 53 //添加第二行 定義表頭 54 row = sheet.CreateRow(1); 55 //單元格賦值 56 row.CreateCell(0).SetCellValue("值"); 57 row.CreateCell(1).SetCellValue("值"); 58 row.CreateCell(2).SetCellValue("值"); 59 row.CreateCell(3).SetCellValue("值"); 60 row.CreateCell(4).SetCellValue("值"); 61 row.CreateCell(5).SetCellValue("值"); 62 63 //設置列寬 64 sheet.SetColumnWidth(0, 60 * 265); 65 sheet.SetColumnWidth(1, 17 * 265); 66 sheet.SetColumnWidth(2, 17 * 265); 67 sheet.SetColumnWidth(3, 17 * 265); 68 sheet.SetColumnWidth(4, 17 * 265); 69 sheet.SetColumnWidth(5, 17 * 265); 70 71 //設置行高 第一行 72 row = sheet.GetRow(0); 73 row.Height = short.Parse((22.5 * 20).ToString()); 74 //使用SetFont方法將字體樣式添加到單元格樣式中 75 cellStyle.SetFont(FontRow); 76 77 //設置行高 第二行 78 row = sheet.GetRow(1); 79 row.Height = short.Parse((18.5 * 20).ToString()); 80 //得到第二行的單元格 81 List<ICell> cells = row.Cells; 82 for (int i = 0; i < cells.Count; i++) 83 { 84 //得到當前行 85 cell = row.GetCell(i); 86 //設置樣式 87 cell.CellStyle = cellStyle; 88 } 89 90 //寫入數據 91 for (int i = 0; i < rowCount; i++) 92 { 93 //建立新行 94 row = sheet.CreateRow(i + 2); 95 //定義新行行高 96 row.Height = short.Parse((13.5 * 20).ToString()); 97 for (int j = 0; j < columnCount; j++) 98 { 99 if (j - 1 >= 0) 100 { 101 //建立新的單元格 102 cell = row.CreateCell(j - 1); 103 //賦值 104 cell.SetCellValue(dt.Rows[i][j].ToString()); 105 cell.CellStyle = cellStyle; 106 } 107 } 108 } 109 string Excelfile = context.Server.MapPath("路徑"); 110 string path = context.Server.MapPath("excel再上一級的路徑"); 111 DirectoryInfo folder = new DirectoryInfo(path); 112 //文件夾是否存在當前Excel 113 foreach (FileInfo file in folder.GetFiles("*.xlsx")) 114 { 115 if (file.FullName == Excelfile) 116 { 117 try 118 { 119 File.Delete(Excelfile); 120 } 121 catch (Exception ex) 122 { 123 Console.Write(ex.Message); 124 } 125 } 126 } 127 using (FileStream file = new FileStream(Excelfile, FileMode.Create)) 128 { 129 workbook.Write(file); //寫入數據 建立文件。 130 file.Close(); 131 } 132 } 133 } 134 catch (Exception ex) 135 { 136 } 137 }