1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
| using NPOI.HSSF.UserModel; using NPOI.HSSF.Util; using NPOI.SS.UserModel; using NPOI.SS.Util; using NPOI.XSSF.UserModel; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Threading.Tasks; using static NPOI.HSSF.Util.HSSFColor;
namespace Helpers { public class ExcelHelper { public static string DataToExcel(DataTable dt, List<ExportDataColumn> columnTitle, string title, string path) { XSSFWorkbook book = new XSSFWorkbook(); XSSFSheet sheet = book.CreateSheet("Export") as XSSFSheet;
CellRangeAddress region = new CellRangeAddress(0, 0, 0, columnTitle.Count - 1); sheet.AddMergedRegion(region);
IFont font12 = book.CreateFont(); font12.FontHeightInPoints = 12; font12.FontName = "微软雅黑"; font12.Boldweight = short.MaxValue; font12.Color = Black.Index;
ICellStyle cellStyle = book.CreateCellStyle(); cellStyle.Alignment = HorizontalAlignment.Center; cellStyle.BorderBottom = BorderStyle.Thin; cellStyle.BorderLeft = BorderStyle.Thin; cellStyle.BorderRight = BorderStyle.Thin; cellStyle.BorderTop = BorderStyle.Thin; cellStyle.SetFont(font12); cellStyle.FillBackgroundColor = BlueGrey.Index;
for (int i = region.FirstRow; i <= region.LastRow; i++) { IRow row = sheet.CreateRow(i); for (int j = region.FirstColumn; j <= region.LastColumn; j++) { ICell singleCell = row.CreateCell((short)j); singleCell.CellStyle = cellStyle; } }
IRow hrow = sheet.GetRow(0); hrow.Height = 20 * 20; ICell icellltop0 = hrow.GetCell(0); icellltop0.CellStyle = cellStyle; icellltop0.SetCellValue(title + "_" + DateTime.Now.ToString("yyyy-MM-dd"));
ICellStyle TitleStyle = book.CreateCellStyle(); TitleStyle.FillForegroundColor = PaleBlue.Index; TitleStyle.FillPattern = FillPattern.SolidForeground; TitleStyle.Alignment = HorizontalAlignment.Center; TitleStyle.BorderBottom = BorderStyle.Thin; TitleStyle.BorderLeft = BorderStyle.Thin; TitleStyle.BorderRight = BorderStyle.Thin; TitleStyle.BorderTop = BorderStyle.Thin;
IRow TitleRow = sheet.CreateRow(1); TitleRow.Height = 20 * 15; for (int i = 0; i < columnTitle.Count; i++) { ICell cell = TitleRow.CreateCell(i); cell.CellStyle = TitleStyle; cell.SetCellValue(columnTitle[i].Label); sheet.SetColumnWidth(i, columnTitle[i].ColumnWidth); }
for (int i = 0; i < dt.Rows.Count; i++) { IRow row = sheet.CreateRow(i + 2); for (int j = 0; j < columnTitle.Count; j++) { row.CreateCell(j).SetCellValue(dt.Rows[i][columnTitle[j].Prop].ToString()); } }
ICellStyle borStyle = book.CreateCellStyle(); borStyle.Alignment = HorizontalAlignment.Center; borStyle.BorderBottom = BorderStyle.Thin; borStyle.BorderLeft = BorderStyle.Thin; borStyle.BorderRight = BorderStyle.Thin; borStyle.BorderTop = BorderStyle.Thin;
for (int i = 1; i <= dt.Rows.Count + 1; i++) { IRow row = sheet.GetRow(i); row.Height = 20 * 16; for (int j = 0; j < columnTitle.Count; j++) { ICell singleCell = row.GetCell((short)j); singleCell.CellStyle = borStyle; } } if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string fileName = title + DateTime.Now.ToString("yyyyMMddHHmmssff") + ".xlsx"; using (FileStream fsWrite = File.OpenWrite($"{path}//{fileName}")) { book.Write(fsWrite); } return fileName; }
public MemoryStream ImportDcExceptionType(string filePath) { IWorkbook workbook = null; FileStream fs; using (fs = new FileStream(filePath, FileMode.Open)) { if (filePath.EndsWith(".xlsx")) { workbook = new XSSFWorkbook(fs); } else if (filePath.EndsWith(".xls")) { workbook = new HSSFWorkbook(fs); } if (workbook != null) { ISheet sheet = workbook.GetSheetAt(0); if (sheet != null) { int rowCount = sheet.LastRowNum; for (int i = 1; i <= rowCount; i++) { IRow row = sheet.GetRow(i); var value = row.GetCell(0)?.ToString();
ICellStyle cellStyle = workbook.CreateCellStyle(); cellStyle.FillForegroundColor = HSSFColor.Red.Index; cellStyle.FillPattern = FillPattern.SolidForeground; row.GetCell(0).CellStyle = cellStyle; } } } MemoryStream memoryStream = new MemoryStream(); workbook.Write(memoryStream); workbook.Close(); return memoryStream; } }
public ICellStyle CellStyle { get; set; } public ExeclTypeEnum ExeclType { get; set; } public IWorkbook Workbook { get; set; } public ISheet Sheet { get; set; } public ICreationHelper Facktory { get; set; } public IDrawing Drawing { get; set; }
public ExcelHelper(string filePath, int sheetIndex) { using (FileStream fs = new FileStream(filePath, FileMode.Open)) { if (filePath.EndsWith(".xlsx")) { ExeclType = ExeclTypeEnum.XLSX; Workbook = new XSSFWorkbook(fs); CellStyle = Workbook.CreateCellStyle(); CellStyle.FillForegroundColor = IndexedColors.Red.Index; CellStyle.FillPattern = FillPattern.SolidForeground; } else { ExeclType = ExeclTypeEnum.XLS; Workbook = new HSSFWorkbook(fs); CellStyle = Workbook.CreateCellStyle(); CellStyle.FillForegroundColor = HSSFColor.Red.Index; CellStyle.FillPattern = FillPattern.SolidForeground; } } Sheet = Workbook.GetSheetAt(sheetIndex); Facktory = Workbook.GetCreationHelper(); Drawing = Sheet.CreateDrawingPatriarch(); }
public void MarkError(IRow row, int indexCell, string msg) { ICell cell = row.GetCell(indexCell); if (cell is null) { cell = row.CreateCell(indexCell); } IClientAnchor anchor = Facktory.CreateClientAnchor();
var comment = Drawing.CreateCellComment(anchor); comment.Author = "Mr.Guan";
if (ExeclTypeEnum.XLSX == ExeclType) { anchor.Col1 = cell.ColumnIndex; anchor.Col2 = cell.ColumnIndex + 1; anchor.Row1 = row.RowNum; anchor.Row2 = row.RowNum + 3; comment.String = new XSSFRichTextString(msg); } else if (ExeclTypeEnum.XLS == ExeclType) { anchor.Col1 = cell.ColumnIndex; anchor.Col2 = cell.ColumnIndex + 3; anchor.Row1 = row.RowNum; anchor.Row2 = row.RowNum + 5; comment.String = new HSSFRichTextString(msg); } cell.CellComment = comment; cell.CellStyle = CellStyle; } }
public enum ExeclTypeEnum { XLSX = 0, XLS = 1 }
public class ExportDataColumn { public string Prop { get; set; } public string Label { get; set; } public int ColumnWidth { get; set; } } }
|