这里是根据已经存在的模板往模板里面写数据。然后导出。
示例
public FileResult GetGenerateExport(string Barcode, Guid? CustomerId, string BranchCode, string Status)
{
try
{
//模板
string configurl = System.Configuration.ConfigurationSettings.AppSettings["TempletsPath"].ToString();
string FileFullPath = Server.MapPath(configurl) + "BranchKey.xls";
if (!System.IO.File.Exists(FileFullPath))
{
return null;
}
HSSFWorkbook workbook = new HSSFWorkbook();
using (FileStream file = new FileStream(FileFullPath, FileMode.Open, FileAccess.Read))
{
workbook = (HSSFWorkbook)WorkbookFactory.Create(file);
}
//需要查询的数据 Str
var model = _IBranchKeyService.GetAllEnt(false).Where(a => a.AtmCustomer != null && a.AtmCustomer.Deleted == false);//客户不能为空,或删除的。
if (!string.IsNullOrEmpty(Barcode))
{
model = model.Where(a => a.Barcode.Contains(Barcode));
}
if (CustomerId.HasValue)
{
model = model.Where(a => a.CustomerId == CustomerId.Value);
}
if (!string.IsNullOrEmpty(BranchCode))
{
model = model.Where(a => a.BranchCode.Contains(BranchCode));
}
if (!string.IsNullOrEmpty(Status))
{
var Enumstatus = (BranchKeyStatus)int.Parse(Status);
model = model.Where(a => a.Status == Enumstatus);
}
var dataList = model.OrderBy(a => a.CustomerName).ThenBy(a => a.BranchCode).ToList();
//需要查询的数据 End
HSSFSheet datasheet = (HSSFSheet)workbook.GetSheetAt(0);
HSSFRow row = null;
var indexRow = 4;
if (dataList.Count > 0)
{
int number = 0;
int SequenceSum = 0;
string SequenceRouteName = "";
string SequenceCustomerName = "";
ICellStyle s = workbook.CreateCellStyle();
s.FillForegroundColor = HSSFColor.RED.index;
s.VerticalAlignment = VerticalAlignment.CENTER;
s.FillPattern = FillPatternType.SOLID_FOREGROUND;
s.Alignment = HorizontalAlignment.CENTER;
s.VerticalAlignment = VerticalAlignment.CENTER;
foreach (var item in dataList)
{
number++;
//创建行
HSSFRow IndexRow = (HSSFRow)datasheet.GetRow(indexRow);
if ((indexRow + 1) < (4 + dataList.Count))
{
row = (HSSFRow)datasheet.CopyRow(indexRow, indexRow + 1);
row.Height = IndexRow.Height;
}
row = (HSSFRow)datasheet.GetRow(indexRow);
row.GetCell(0).SetCellValue(number);
row.GetCell(1).SetCellValue(item.Barcode);
row.GetCell(2).SetCellValue(item.CustomerName);
row.GetCell(3).SetCellValue(item.BranchCode);
row.GetCell(4).SetCellValue(item.BranchName);
int Countofkey = GetCountOfKey(ref SequenceSum, ref SequenceRouteName, item.BranchCode, ref SequenceCustomerName, item.CustomerName);
row.GetCell(5).SetCellValue(Countofkey.ToStr());
row.GetCell(6).SetCellValue(item.ServiceType == CustomerTypes.CustomerTypes_Other ? "" : item.ServiceType.GetString().ToStr());
row.GetCell(7).SetCellValue(item.Status == BranchKeyStatus.Node ? "" : item.Status.GetString().ToStr());
if (item.Status == BranchKeyStatus.Damaged)
{
ICell mcell = row.GetCell(7);
row.GetCell(7).CellStyle = s;
}
row.GetCell(8).SetCellValue(item.CreatedDate);
indexRow++;
}
}
workbook.ForceFormulaRecalculation = true;
System.IO.MemoryStream ms = new System.IO.MemoryStream();
workbook.Write(ms);
ms.Seek(0, SeekOrigin.Begin);
DateTime dt = DateTime.Now;
string dateTime = dt.ToString("yyMMdd");//yyMMddHHmmssfff
string fileName = "BranchKey" + dateTime + ".xls";
return File(ms, "application/vnd.ms-excel", fileName);
}
catch (Exception ex)
{
throw;
}
}
需要下载的DLL
https://download.csdn.net/download/xiongliuyun/20432621