public void PrintExcel(List datalist, List zuzhilist, List chuwailist)
{
//创建excel
string filename = DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";
string newpath = Server.MapPath("/excel/PrintExcel/") + filename;
FileInfo newfile = new FileInfo(newpath);
ExcelPackage package = new ExcelPackage(newfile);
ExcelWorkbook workBook = package.Workbook;
ExcelWorksheet sheetInOut = package.Workbook.Worksheets.Add("工作簿名称");
//设置工作簿比例
sheetInOut.View.ZoomScale = 60;
//设置初始字体大小、字体类型、排列方式
sheetInOut.Cells.Style.Font.Size = 10;
sheetInOut.Cells.Style.Font.Name = "微软雅黑";
sheetInOut.Cells.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
sheetInOut.Cells.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
//合并单元格,附带填充内容
void mergeCells(int r1, int c1, int r2, int c2, string value, bool bISFont, ExcelWorksheet sheet,int intFontSize,string strAlignType)
{
ExcelRange range = sheet.Cells[r1, c1, r2, c2];
range.Merge = true;//合并单元格
//左对齐
if (strAlignType == "L")range.Style.HorizontalAlignment = ExcelHorizontalAlignment.Left;
//右对齐
else if (strAlignType == "R")range.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
else
{
range.Style.VerticalAlignment = ExcelVerticalAlignment.Center;//居中
range.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
}
//单元格赋值 有时候一合并单元格值就没有了 重新赋值一遍
if (!string.IsNullOrEmpty(value)) range.Value = value;
if (bISFont) range.Style.Font.Bold = true;//字体加粗
if(intFontSize !=0)
range.Style.Font.Size = intFontSize; //字号
else
range.Style.Font.Size = 10;
}
//确定表格 在里面填充数据索引(无论行、列)以0开始
Array arrInOut = Array.CreateInstance(typeof(object), datalist.Count + 6, 8);//标题行+数据行 导出列数
arrInOut.SetValue("**********联络表", 0, 0);//index1-行 index2-列
arrInOut.SetValue("①*********情报", 1, 0);
arrInOut.SetValue("※新人**************************************变更", 2, 0);
arrInOut.SetValue("※员************************************** ***变更;", 3, 0);
arrInOut.SetValue("No.", 4, 0);
arrInOut.SetValue("姓名", 4, 1);
arrInOut.SetValue("区分", 4, 2);
arrInOut.SetValue("所属", 4, 3);
arrInOut.SetValue("入社情报", 4, 4);
arrInOut.SetValue("入社时间", 5, 4);
arrInOut.SetValue("备注", 5, 5);
arrInOut.SetValue("离职情报", 4, 6);
arrInOut.SetValue("离职时间", 5, 6);
arrInOut.SetValue("说明", 5, 7);
//填充数据
int i = 6;
datalist.ForEach(x =>
{
arrInOut.SetValue(x.EmployeeNo, i, 0);
arrInOut.SetValue(x.UserName, i, 1);
arrInOut.SetValue(getrutuidhequfen(x.InOutType), i, 2);
arrInOut.SetValue(amService.GetFullDeptNameA(x.DepartmentID.ToString()), i, 3);
if(x.InOutType == (int)OutType.In)
{
arrInOut.SetValue(x.InOutDate.ToShortDateString(), i, 4);
arrInOut.SetValue(x.Memo, i, 5);
}
else if (x.InOutType == (int)OutType.Out)
{
arrInOut.SetValue(x.InOutDate.ToShortDateString(), i, 6);
arrInOut.SetValue(x.Memo, i, 7);
}
i++;
});
//合并单元格 合并的是EXCEL中的表格 索引是从1开始
mergeCells(1, 1, 1, 8, "", true, sheetInOut, 18, "");
mergeCells(2, 1, 2, 8, "", true, sheetInOut, 12, "L");
mergeCells(3, 1, 3, 8, "", false, sheetInOut, 9, "R");
mergeCells(4, 1, 4, 8, "", false, sheetInOut, 9, "R");
mergeCells(5, 1, 6, 1, "", false, sheetInOut, 0, "");
mergeCells(5, 2, 6, 2, "", false, sheetInOut, 0, "");
mergeCells(5, 3, 6, 3, "", false, sheetInOut, 0, "");
mergeCells(5, 4, 6, 4, "", false, sheetInOut, 0, "");
mergeCells(5, 5, 5, 6, "", false, sheetInOut, 0, "");
mergeCells(5, 7, 5, 8, "", false, sheetInOut, 0, "");
try
{
ExcelRange rangeInOut = sheetInOut.Cells[1, 1, datalist.Count + 6, 8];
rangeInOut.Value = arrInOut;
for (int g = 1; g <= datalist.Count; g++){sheetInOut.Column(g).Width = 20;}
//“所属”以及“备注”、”说明“列设置宽35
sheetInOut.Cells[7, 4, datalist.Count + 6, 4].AutoFitColumns(35);
sheetInOut.Cells[7, 6, datalist.Count + 6, 6].AutoFitColumns(35);
sheetInOut.Cells[7, 8, datalist.Count + 6, 8].AutoFitColumns(35);
//弹出提示是否保存
MemoryStream ms = new MemoryStream();
package.SaveAs(ms);
Response.ContentType = "application/vnd.ms-excel";
string fileName = HttpUtility.UrlEncode(filename);
Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", fileName));
Response.Buffer = true;
Response.Clear();
Response.BinaryWrite(ms.GetBuffer());
HttpContext.Current.ApplicationInstance.CompleteRequest();
//这种也可以设置工作簿的比例,不过发布到IIS上出错,本地可以Mark下
package.SaveAs(newfile);//保存文件
//实例化Workbook类,加载Excel测试文档
Spire.Xls.Workbook workbook = new Spire.Xls.Workbook();
workbook.LoadFromFile(newfile.FullName);
workbook.ConverterSetting.SheetFitToPage = true;
//获取第一个工作表
Spire.Xls.Worksheet sheetSpire = workbook.Worksheets[0];
//设置视图缩放比例
sheetSpire.Zoom = 60;
//保存并打开文档
string strExcel = newfile.DirectoryName + "/" + fileName.Substring(0, fileName.LastIndexOf('.')) + "_Result.xlsx";
workbook.SaveToFile(strExcel, ExcelVersion.Version2010);
System.Diagnostics.Process.Start(strExcel);
}
catch(Exception exp)
{
throw new Exception(exp.Message);
}
}
附:
//表单整体颜色
sheetInOut.Cells.Style.Fill.PatternType = ExcelFillStyle.Solid;
sheetInOut.Cells.Style.Fill.BackgroundColor.SetColor(Color.White);
//自动换行
sheet.Cells[a,b].Style.WrapText= true;
//单元格自动适应大小
sheet.Cells.Style.ShrinkToFit = true;
//设置边框
void setborder(ExcelRange r)
{
r.Style.Border.Left.Style = ExcelBorderStyle.Thin;
r.Style.Border.Right.Style = ExcelBorderStyle.Thin;
r.Style.Border.Top.Style = ExcelBorderStyle.Thin;
r.Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
}
//设置背景颜色-黄色 设置边框及其颜色
void BgColorY(ExcelRange r)
{
r.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(255, 255, 0));
r.Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(255, 255, 0));
}
//设置日期格式
sheetInOut.Column(1).Style.Numberformat.Format = "DD/MM/YY";
sheetInOut.Cells[1, 1].Value = DataTime.Now;