前几天由于公司需求,要将配置文件中的dd是的图片以及相关信息导出成excel。在此具体需求如下
1、根据ani配置文件读取配置信息(配置说明、配置标题、配置路径、配置图片、图片尺寸)
2、根据dds图片路径导入excel(原图大小显示、一行一张图)图片显示在单个单元格中
3、图片高度高于单元格行高限制进行缩放显示(行高:409)
4、首行背景色填充+冻结
注意:
1、配置文件数据量比较大,excel写入的时候容易引发内存溢出,特别是内存达到1g的时候就报错了
2、都要将dds文件读取到内存中,图片大小又不相同。还要缩放,记得释放图片。以及数据流,要不然容易引发:GDI+一般错误什么鬼的
3、我也使用NPOI的方式,也使用过Aspose.Cells,两个差不多,记得使用NuGet搜索一下关键字,安装添加引用即可
代码也没整理,你可以根据上面的几个点拆分成各个方法。
///
/// 解析配置文件并导出Excel
///
///
///
///
public string ExportExcel(Dictionary> dicAniInfo, string[] listKeys)
{
Workbook wb = new Workbook();
wb.Worksheets.Clear();
Worksheet ws = wb.Worksheets.Add("物品资源列表");
ws.FreezePanes(1, 5, 1, 5);
//表头
for (int i = 0; i < this.SGridRetrieve.PrimaryGrid.Columns.Count; i++)
{
ws.Cells[0, i].PutValue(this.SGridRetrieve.PrimaryGrid.Columns[i].Name);
//设置样式
Style style = ws.Cells[0, i].GetStyle();
style.ForegroundColor = Color.Orange;//背景色
style.Pattern = BackgroundType.Solid;
style.Font.IsBold = true;
ws.Cells[0, i].SetStyle(style);
}
//设置各列的宽度
ws.Cells.SetColumnWidth(0, 30);
ws.Cells.SetColumnWidth(1, 48);
ws.Cells.SetColumnWidth(2, 57);
ws.Cells.SetColumnWidth(3, 77);
ws.Cells.SetColumnWidth(4, 14);
int rowIndex = 1;
for (int a = 0; a < listKeys.Length; a++)
{
bool isNote = dicAniInfo[listKeys[a]][0].IndexOf("//") >= 0 ? true : false;//是否又注释
for (int b = 2; b < dicAniInfo[listKeys[a]].Count; b++)
{
if (dicAniInfo[listKeys[a]][b].Trim() == "" || (dicAniInfo[listKeys[a]][b].Trim().IndexOf("//") >= 0 && dicAniInfo[listKeys[a]][b].Trim().IndexOf("[") < 0)) continue;
//设置单元格样式
Style style = ws.Cells.Rows[rowIndex].Style;
style.VerticalAlignment = TextAlignmentType.Top;//置顶,靠左
ws.Cells.Style = (style);
//数据填充
if (isNote && b == 2)
{
int t = dicAniInfo[listKeys[a]][0].IndexOf("[");//注释与title分离
ws.Cells[rowIndex, 0].PutValue(dicAniInfo[listKeys[a]][0].Substring(0, t));
ws.Cells[rowIndex, 1].PutValue(dicAniInfo[listKeys[a]][0].Substring(t));
}
else if (b == 2)
{
ws.Cells[rowIndex, 1].PutValue(dicAniInfo[listKeys[a]][0]);
}
int _index = dicAniInfo[listKeys[a]][b].IndexOf("=") + 1;
string dds = dicAniInfo[listKeys[a]][b].Substring(_index).Replace("\r", "").Replace("/", "\\");//去除\r,与转换反斜杠
ws.Cells[rowIndex, 2].PutValue(dds);
//将dds转为Bitmap图片(内存中)
using (Bitmap img = GetPicture(ResPath + dds))
{
if (img != null)
{
ws.Cells[rowIndex, 4].PutValue(img.Width + " * " + img.Height);
short height = 0;
if (img.Height <= 16)//设置行高最低13
{
height = 13;
}
else if (img.Height * 0.75 > 409.5)//行高超出409,缩放显示
{
height = 409;
int _width = Convert.ToInt16(409.00 / Convert.ToInt16(img.Height) * img.Width);
Bitmap _img = new Bitmap(_width, height);
Graphics g = Graphics.FromImage(_img);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(img, new Rectangle(0, 0, _width, 409), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);
g.Dispose();//释放
AddPicToExcel(ws, _img, rowIndex);//将图片插入至单元格中
}
else
{
height = Convert.ToInt16(img.Height * 0.75);
AddPicToExcel(ws, img, rowIndex);
}
ws.Cells.SetRowHeight(rowIndex, height);//设置行高
//ws.Cells[rowIndex, 4].PutValue(img.Width + " * " + img.Height);
//ws.Cells.SetRowHeight(rowIndex, img.Height <= 16 ? Convert.ToInt16(13) : Convert.ToInt16(img.Height * 0.75) > 409.5 ? 409 : img.Height * 0.75);
//AddPicToExcel(ws, img, rowIndex);
}
}
rowIndex++;
}
}
GC.Collect();//清理内存
string saveFileName = OutputPath + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";
wb.Save(saveFileName);
return saveFileName;
}
///
/// 添加图片至指定的单元格中
///
///
///
///
public static void AddPicToExcel(Worksheet ws, Bitmap img, int row)
{
using (MemoryStream ms = new MemoryStream())
{
img.Save(ms, ImageFormat.Png);
Aspose.Cells.Drawing.PictureCollection pictures = ws.Pictures;
pictures.Add(row, 3, ms);
}
}