using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.Drawing;
namespace SmartReleaseApi.Utils
{
//下载到Excel的公共方法
public class DownloadToExcelUtil
{
public static string DownloadToExcel(string[] headerNames,Dictionary<string,List<string>> values,string fileName)
{
HSSFWorkbook transWorkbook = new HSSFWorkbook();
//自定义颜色
HSSFPalette palette = transWorkbook.GetCustomPalette();
//纬创蓝 先加入到色板中 RGB颜色值,第一个值:8~64之间,后面三个值为RGB色值
palette.SetColorAtIndex((short)8, (byte)0, (byte)80, (byte)110);
Color color = Color.FromArgb(0, 80, 110);
var myColor = palette.FindColor(color.R, color.G, color.B);
//表头样式
ICellStyle transStyle = transWorkbook.CreateCellStyle();
transStyle.Alignment = HorizontalAlignment.CenterSelection;
IFont headerfont = transWorkbook.CreateFont();
headerfont.FontName = "微软雅黑";//字体
headerfont.FontHeightInPoints = 12;//字体大小
headerfont.IsBold = true;//是否加粗
headerfont.Color = 9; //表头白色
transStyle.SetFont(headerfont);
transStyle.FillForegroundColor = myColor.Indexed;
transStyle.FillPattern = FillPattern.SolidForeground;
ISheet transSheet = transWorkbook.CreateSheet("sheet");
IRow transRowHead = transSheet.CreateRow(0);
//存储最大列宽
Dictionary<int, int> maxWidth = new Dictionary<int, int>();
for (int i = 0; i < headerNames.Length; i++) {
ICell ccell = transRowHead.CreateCell(i);
ccell.SetCellValue(headerNames[i]);
ccell.CellStyle = transStyle;
//表格宽度自适应
getMaxWidth(maxWidth, ccell, i);
}
//表体样式
ICellStyle bodyStyle = transWorkbook.CreateCellStyle();
bodyStyle.Alignment = HorizontalAlignment.CenterSelection;
IFont bodyFont = transWorkbook.CreateFont();
bodyFont.FontName = "微软雅黑";//字体
bodyFont.FontHeightInPoints = 10;
bodyFont.IsBold = false;//是否加粗
bodyStyle.SetFont(bodyFont);
var trnsCount = 1;
List<string> vkeys = values.Keys.ToList();
foreach (var item in vkeys)
{
IRow trnsRow = transSheet.CreateRow(trnsCount);
List<string> valueList = values[item];
for (int k = 0; k < valueList.Count; k++) {
ICell createCell = trnsRow.CreateCell(k);
createCell.SetCellValue(valueList[k]);
//表格宽度自适应
getMaxWidth(maxWidth,createCell,k);
createCell.CellStyle = bodyStyle;
}
trnsCount++;
}
if (null != values && values.Count > 0) {
for (int i = 0; i < headerNames.Length; i++)
{
transSheet.SetColumnWidth(i, maxWidth[i]);
}
}
string filePath = Environment.CurrentDirectory + @"\wwwroot\UploadTemp\" + UUID.Getuuid() + ".xls";
if (!string.IsNullOrEmpty(fileName)) {
filePath = Environment.CurrentDirectory + @"\wwwroot\UploadTemp\" + fileName + ".xls";
}
if (File.Exists(filePath))
{
File.Delete(filePath);
}
using (FileStream maurl = File.OpenWrite(filePath))
{
transWorkbook.Write(maurl);
transWorkbook.Close();
}
return filePath;
}
private static void getMaxWidth(Dictionary<int, int> maxWidth,ICell cell,int key) {
//表格宽度自适应
int length = System.Text.Encoding.Default.GetBytes(cell.StringCellValue).Count() * 256 + 200;
//这里把宽度最大限制到15000
if (length > 15000)
{
length = 15000;
}
if (!maxWidth.ContainsKey(key))
{
maxWidth.Add(key, Math.Max(length, 0));
}
else
{
maxWidth[key] = Math.Max(length, maxWidth[key]);
}
}
}
}