62,046
社区成员
发帖
与我相关
我的任务
分享
public MemoryStream ExportQualifiedExaminees(string modelPath, string serviceName, IList<QualifiedExamineeEn> lst, string imgPath)
{
MemoryStream imageStream = new MemoryStream();
HSSFWorkbook workbook = new HSSFWorkbook(File.OpenRead(modelPath));
Image.FromFile(imgPath).Save(imageStream, System.Drawing.Imaging.ImageFormat.Png);
byte[] bytes = new byte[imageStream.Length];
bytes = imageStream.GetBuffer();
imageStream = null;
//int picInt = ;
HSSFClientAnchor ac = new HSSFClientAnchor(0, 0, 0, 0, 0, 0, 1, 1);
Row r = workbook.GetSheet("合格考生名单").GetRow(4);
var style = r.GetCell(0).CellStyle;
var type = r.GetCell(0).CellType;
IEnumerable<IGrouping<string, QualifiedExamineeEn>> list = lst.GroupBy(m => m.ExamLocationName);
for (int i = 0; i < list.Count(); i++)
{
List<QualifiedExamineeEn> ls = list.ElementAt(i).ToList<QualifiedExamineeEn>();
// 就是这里new了一些新的sheet,虽然是以克隆模板的方式。
Sheet sheet = workbook.CloneSheet(0);
workbook.SetSheetName(i + 1, ls[0].ExamLocationName);
var patriarch = sheet.CreateDrawingPatriarch();
patriarch.CreatePicture(ac, workbook.AddPicture(bytes, PictureType.PNG)).Resize();
sheet.GetRow(2).GetCell(0).SetCellValue("考试服务名称:" + serviceName);
sheet.GetRow(3).GetCell(0).SetCellValue("考场地址:" + ls[0].LocationAddress);
int countFlag = 1;
foreach (var m in ls)
{
var newRow = sheet.CreateRow(sheet.LastRowNum + 1);
newRow.HeightInPoints = 18;
if (m.IDNumber == null || m.IDNumber.Length == 0)
{
CreateCell(newRow, 0, style, type, countFlag.ToString());
CreateCell(newRow, 1, style, type, m.AdmissionFormId.ToString());
CreateCell(newRow, 2, style, type, "");
CreateCell(newRow, 3, style, type, "");
CreateCell(newRow, 4, style, type, "");
CreateCell(newRow, 5, style, type, "");
CreateCell(newRow, 6, style, type, "");
CreateCell(newRow, 7, style, type, "");
CreateCell(newRow, 8, style, type, "");
}
else
{
CreateCell(newRow, 0, style, type, countFlag.ToString());
CreateCell(newRow, 1, style, type, m.AdmissionFormId.ToString());
CreateCell(newRow, 2, style, type, m.FirstName + " " + m.LastName);
CreateCell(newRow, 3, style, type, m.Gender ? "男" : "女");
CreateCell(newRow, 4, style, type, m.Birthday);
CreateCell(newRow, 5, style, type, m.Department);
CreateCell(newRow, 6, style, type, m.IDNumber);
CreateCell(newRow, 7, style, type, "");
CreateCell(newRow, 8, style, type, "");
}
countFlag++;
}
}
workbook.RemoveSheetAt(0);
MemoryStream ms = new MemoryStream();
workbook.Write(ms);
workbook = null;
r = null;
return ms;
}