|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.IO; |
| 6 | +using NPOI.HSSF.UserModel; |
| 7 | +using System.Windows.Forms; |
| 8 | +using NPOI.SS.UserModel; |
| 9 | +using NPOI.HSSF.Util; |
| 10 | + |
| 11 | +namespace HFSIFrame.UC |
| 12 | +{ |
| 13 | + public class ExportExcel |
| 14 | + { |
| 15 | + public static void GridToExcel(string fileName, DataGridView dgv) |
| 16 | + { |
| 17 | + if (dgv.Rows.Count == 0) |
| 18 | + { |
| 19 | + return; |
| 20 | + } |
| 21 | + SaveFileDialog sfd = new SaveFileDialog(); |
| 22 | + sfd.Filter = "Excel 2003格式|*.xls"; |
| 23 | + sfd.FileName = fileName + DateTime.Now.ToString("yyyyMMddHHmmssms"); |
| 24 | + if (sfd.ShowDialog() != DialogResult.OK) |
| 25 | + { |
| 26 | + return; |
| 27 | + } |
| 28 | + HSSFWorkbook wb = new HSSFWorkbook(); |
| 29 | + HSSFSheet sheet = (HSSFSheet)wb.CreateSheet(fileName); |
| 30 | + HSSFRow headRow = (HSSFRow)sheet.CreateRow(0); |
| 31 | + |
| 32 | + //样式 |
| 33 | + ICellStyle cellStyle = Getcellstyle(wb); |
| 34 | + |
| 35 | + for (int i = 0; i < dgv.Columns.Count; i++) |
| 36 | + { |
| 37 | + HSSFCell headCell = (HSSFCell)headRow.CreateCell(i, CellType.String); |
| 38 | + headCell.SetCellValue(dgv.Columns[i].HeaderText); |
| 39 | + headCell.CellStyle = cellStyle; |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | + for (int i = 0; i < dgv.Rows.Count; i++) |
| 46 | + { |
| 47 | + HSSFRow row = (HSSFRow)sheet.CreateRow(i + 1); |
| 48 | + for (int j = 0; j < dgv.Columns.Count; j++) |
| 49 | + { |
| 50 | + HSSFCell cell = (HSSFCell)row.CreateCell(j); |
| 51 | + if (dgv.Rows[i].Cells[j].Value == null) |
| 52 | + { |
| 53 | + cell.SetCellType(CellType.Blank); |
| 54 | + } |
| 55 | + else |
| 56 | + { |
| 57 | + if (dgv.Rows[i].Cells[j].ValueType.FullName.Contains("System.Int32")) |
| 58 | + { |
| 59 | + cell.SetCellValue(Convert.ToInt32(dgv.Rows[i].Cells[j].Value)); |
| 60 | + } |
| 61 | + else if (dgv.Rows[i].Cells[j].ValueType.FullName.Contains("System.String")) |
| 62 | + { |
| 63 | + cell.SetCellValue(dgv.Rows[i].Cells[j].Value.ToString()); |
| 64 | + } |
| 65 | + else if (dgv.Rows[i].Cells[j].ValueType.FullName.Contains("System.Single")) |
| 66 | + { |
| 67 | + cell.SetCellValue(Convert.ToSingle(dgv.Rows[i].Cells[j].Value)); |
| 68 | + } |
| 69 | + else if (dgv.Rows[i].Cells[j].ValueType.FullName.Contains("System.Double")) |
| 70 | + { |
| 71 | + cell.SetCellValue(Convert.ToDouble(dgv.Rows[i].Cells[j].Value)); |
| 72 | + } |
| 73 | + else if (dgv.Rows[i].Cells[j].ValueType.FullName.Contains("System.Decimal")) |
| 74 | + { |
| 75 | + cell.SetCellValue(Convert.ToDouble(dgv.Rows[i].Cells[j].Value)); |
| 76 | + } |
| 77 | + else if (dgv.Rows[i].Cells[j].ValueType.FullName.Contains("System.DateTime")) |
| 78 | + { |
| 79 | + cell.SetCellValue(Convert.ToDateTime(dgv.Rows[i].Cells[j].Value).ToString("yyyy-MM-dd")); |
| 80 | + } |
| 81 | + } |
| 82 | + //cell.CellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin; |
| 83 | + //cell.CellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin; |
| 84 | + //cell.CellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; |
| 85 | + //cell.CellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin; |
| 86 | + cell.CellStyle = cellStyle; |
| 87 | + } |
| 88 | + |
| 89 | + } |
| 90 | + for (int i = 0; i < dgv.Columns.Count; i++) |
| 91 | + { |
| 92 | + sheet.AutoSizeColumn(i); |
| 93 | + } |
| 94 | + using (FileStream fs = new FileStream(sfd.FileName, FileMode.Create)) |
| 95 | + { |
| 96 | + wb.Write(fs); |
| 97 | + } |
| 98 | + HFSMessageBox.ShowMessage("导出成功!"); |
| 99 | + //MessageBox.Show("导出成功!", "导出提示", MessageBoxButtons.OK, MessageBoxIcon.Information); |
| 100 | + } |
| 101 | + |
| 102 | + public enum stylexls |
| 103 | + { |
| 104 | + |
| 105 | + 头, |
| 106 | + |
| 107 | + url, |
| 108 | + |
| 109 | + 时间, |
| 110 | + |
| 111 | + 数字, |
| 112 | + |
| 113 | + 钱, |
| 114 | + |
| 115 | + 百分比, |
| 116 | + |
| 117 | + 中文大写, |
| 118 | + |
| 119 | + 科学计数法, |
| 120 | + |
| 121 | + 默认 |
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + static ICellStyle Getcellstyle(IWorkbook wb, stylexls str=stylexls.默认) |
| 127 | + { |
| 128 | + |
| 129 | + ICellStyle cellStyle = wb.CreateCellStyle(); |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | + //定义几种字体 |
| 135 | + |
| 136 | + //也可以一种字体,写一些公共属性,然后在下面需要时加特殊的 |
| 137 | + |
| 138 | + IFont font12 = wb.CreateFont(); |
| 139 | + |
| 140 | + font12.FontHeightInPoints = 10; |
| 141 | + |
| 142 | + font12.FontName = "微软雅黑"; |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | + IFont font = wb.CreateFont(); |
| 150 | + |
| 151 | + font.FontName = "微软雅黑"; |
| 152 | + |
| 153 | + //font.Underline = 1;下划线 |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | + |
| 160 | + |
| 161 | + IFont fontcolorblue = wb.CreateFont(); |
| 162 | + |
| 163 | + fontcolorblue.Color = HSSFColor.OliveGreen.Black.Index; |
| 164 | + |
| 165 | + fontcolorblue.IsItalic = true;//下划线 |
| 166 | + |
| 167 | + fontcolorblue.FontName = "微软雅黑"; |
| 168 | + |
| 169 | + |
| 170 | + |
| 171 | + |
| 172 | + |
| 173 | + |
| 174 | + |
| 175 | + //边框 |
| 176 | + |
| 177 | + cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin; |
| 178 | + |
| 179 | + cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; |
| 180 | + |
| 181 | + cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin; |
| 182 | + |
| 183 | + cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin; |
| 184 | + |
| 185 | + //边框颜色 |
| 186 | + |
| 187 | + cellStyle.BottomBorderColor = HSSFColor.OliveGreen.Black.Index; |
| 188 | + |
| 189 | + cellStyle.TopBorderColor = HSSFColor.OliveGreen.Black.Index; |
| 190 | + |
| 191 | + |
| 192 | + |
| 193 | + |
| 194 | + //背景图形,我没有用到过。感觉很丑 |
| 195 | + |
| 196 | + //cellStyle.FillBackgroundColor = HSSFColor.OLIVE_GREEN.BLUE.index; |
| 197 | + |
| 198 | + //cellStyle.FillForegroundColor = HSSFColor.OLIVE_GREEN.BLUE.index; |
| 199 | + |
| 200 | + cellStyle.FillForegroundColor = HSSFColor.White.Index; |
| 201 | + |
| 202 | + // cellStyle.FillPattern = FillPatternType.NO_FILL; |
| 203 | + |
| 204 | + cellStyle.FillBackgroundColor = HSSFColor.Maroon.Index; |
| 205 | + |
| 206 | + |
| 207 | + |
| 208 | + //水平对齐 |
| 209 | + |
| 210 | + cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left; |
| 211 | + |
| 212 | + |
| 213 | + |
| 214 | + |
| 215 | + //垂直对齐 |
| 216 | + |
| 217 | + cellStyle.VerticalAlignment = VerticalAlignment.Center; |
| 218 | + |
| 219 | + |
| 220 | + |
| 221 | + |
| 222 | + //自动换行 |
| 223 | + |
| 224 | + cellStyle.WrapText = true; |
| 225 | + |
| 226 | + |
| 227 | + |
| 228 | + |
| 229 | + //缩进;当设置为1时,前面留的空白太大了。希旺官网改进。或者是我设置的不对 |
| 230 | + |
| 231 | + cellStyle.Indention = 0; |
| 232 | + |
| 233 | + |
| 234 | + return cellStyle; |
| 235 | + |
| 236 | + //上面基本都是设共公的设置 |
| 237 | + |
| 238 | + //下面列出了常用的字段类型 |
| 239 | + |
| 240 | + switch (str) |
| 241 | + { |
| 242 | + |
| 243 | + case stylexls.头: |
| 244 | + |
| 245 | + // cellStyle.FillPattern = FillPatternType.LEAST_DOTS; |
| 246 | + |
| 247 | + cellStyle.SetFont(font12); |
| 248 | + |
| 249 | + break; |
| 250 | + |
| 251 | + case stylexls.时间: |
| 252 | + |
| 253 | + IDataFormat datastyle = wb.CreateDataFormat(); |
| 254 | + |
| 255 | + |
| 256 | + |
| 257 | + |
| 258 | + cellStyle.DataFormat = datastyle.GetFormat("yyyy/mm/dd"); |
| 259 | + |
| 260 | + cellStyle.SetFont(font); |
| 261 | + |
| 262 | + break; |
| 263 | + |
| 264 | + case stylexls.数字: |
| 265 | + |
| 266 | + cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00"); |
| 267 | + |
| 268 | + cellStyle.SetFont(font); |
| 269 | + |
| 270 | + break; |
| 271 | + |
| 272 | + case stylexls.钱: |
| 273 | + |
| 274 | + IDataFormat format = wb.CreateDataFormat(); |
| 275 | + |
| 276 | + cellStyle.DataFormat = format.GetFormat("¥#,##0"); |
| 277 | + |
| 278 | + cellStyle.SetFont(font); |
| 279 | + |
| 280 | + break; |
| 281 | + |
| 282 | + case stylexls.url: |
| 283 | + |
| 284 | + fontcolorblue.Underline = FontUnderlineType.Single; |
| 285 | + |
| 286 | + cellStyle.SetFont(fontcolorblue); |
| 287 | + |
| 288 | + break; |
| 289 | + |
| 290 | + case stylexls.百分比: |
| 291 | + |
| 292 | + cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%"); |
| 293 | + |
| 294 | + cellStyle.SetFont(font); |
| 295 | + |
| 296 | + break; |
| 297 | + |
| 298 | + case stylexls.中文大写: |
| 299 | + |
| 300 | + IDataFormat format1 = wb.CreateDataFormat(); |
| 301 | + |
| 302 | + cellStyle.DataFormat = format1.GetFormat("[DbNum2][$-804]0"); |
| 303 | + |
| 304 | + cellStyle.SetFont(font); |
| 305 | + |
| 306 | + break; |
| 307 | + |
| 308 | + case stylexls.科学计数法: |
| 309 | + |
| 310 | + cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00E+00"); |
| 311 | + |
| 312 | + cellStyle.SetFont(font); |
| 313 | + |
| 314 | + break; |
| 315 | + |
| 316 | + case stylexls.默认: |
| 317 | + |
| 318 | + cellStyle.SetFont(font); |
| 319 | + |
| 320 | + break; |
| 321 | + |
| 322 | + } |
| 323 | + |
| 324 | + return cellStyle; |
| 325 | + } |
| 326 | + |
| 327 | + |
| 328 | + } |
| 329 | +} |
0 commit comments