1.生成存放目录
// 文件生成后存放的目录(临时文件存入ChunkUploadTemp目录)
var temporaryDirPath = Path.Combine(new VaultFactory().GetBaseDirectory(),ChunkUploadFileBucket);
2.生成表格
// 生成表格
var productPath = HandExcel.ProductComponentExtendHistoty(componentExtendHistoty,temporaryDirPath);
//判断地址是否存在
if (!Directory.Exists(temporaryDirPath))
{
Directory.CreateDirectory(temporaryDirPath);
}
string id = string.Empty;
string Name = string.Empty;
// 创建文档
Workbook work = new Workbook(FileFormatType.Xlsx);
Worksheet sheet = work.Worksheets[0];
// 写表头
List<string> headNameList = new List<string>() { "名称", "值", "更新时间", "更新人", "更新描述" };
for (int i = 0; i < headNameList.Count; i++)
{
string item = headNameList[i];
Cell cell = sheet.Cells[0, i];
cell.PutValue(item);
}
// 写表体(i+1表示从第二行开始)
for (int i = 0; i < component.Count; i++)
{
Compent item = component[i];
// 参数名称
Cell cell_01 = sheet.Cells[i + 1, 0];
cell_01.PutValue(item.Name);
// 新参数值
Cell cell_02 = sheet.Cells[i + 1, 1];
cell_02.PutValue(item.NewVal + item.Unit);
// 更新时间
Cell cell_03 = sheet.Cells[i + 1, 2];
cell_03.PutValue(item.UpdateTimeStr);
// 更新人
Cell cell_04 = sheet.Cells[i + 1, 3];
cell_04.PutValue(item.UpdateUserName);
// 更新描述
Cell cell_05 = sheet.Cells[i + 1, 4];
cell_05.PutValue(item.UpdateDescStr);
// 用于动态生成文件名称
if (i == 0)
{
id = item.id;
Name = item.Name;
}
}
// 另存为
string fileName = string.Format("xxx记录{0}({1}).xlsx", id, Name);
string fullPath = Path.Combine(temporaryDirPath, fileName);
work.Save(fullPath);
// 返回fullPath
return fullPath;
3下载
// 返回stream
string fileName = Path.GetFileName(productPath);
HandStream.DownloadStream(fileName, productPath);
// 删除原文件
FileInfo f = new FileInfo(productPath);
f.Delete();