C# 数据写入excel文件并下载

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();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值