C#生成Excel保存到服务器端并下载
using MongoDB.Bson;
using Newtonsoft.Json.Linq;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using Zluo.CMember.Entity;
using Zluo.CMember.Interface;
using Zluo.CMember.Service;
using Zluo.CMember.Web.RequestParam;
using Zluo.CMember.Web.RequestParam.Account;
using Zluo.CMember.Web.RequestParam.Order;
using Zluo.Common;
using Zluo.Common.CMember;
using Zluo.SessionCached;
public void CreateExport()
{
try
{
string fileName = "ErrorSongIds.xls"; // 文件名称
string filePath = Path.Combine(HttpRuntime.AppDomainAppPath, "Upload") + "/" + fileName;
// 1.检测是否存在文件夹,若不存在就建立个文件夹
string directoryName = Path.GetDirectoryName(filePath); if (!Directory.Exists(directoryName)) { Directory.CreateDirectory(directoryName); }
// 2.解析单元格头部,设置单元头的中文名称
HSSFWorkbook workbook = new HSSFWorkbook();
// 工作簿
ISheet sheet = workbook.CreateSheet("失败歌曲");
// 工作表
IRow row = sheet.CreateRow(); row.CreateCell().SetCellValue("原始ID");
row.CreateCell().SetCellValue("歌曲名称");
row.CreateCell().SetCellValue("歌手名");
row.CreateCell().SetCellValue("失败原因");
//_songListService.getErrorExcel(uid);
BsonArray array = null;
int rowIndex = ; BsonDocument bd = null;
if (array != null && array.Count > )
{
for (int i = ; i < array.Count; i++)
{
IRow rowTmp = sheet.CreateRow(rowIndex);
bd = (BsonDocument)array[i];
if (bd != null)
{
rowTmp.CreateCell().SetCellValue(bd.GetValue("sourceId").ToString());
rowTmp.CreateCell().SetCellValue(bd.GetValue("songName").ToString());
rowTmp.CreateCell().SetCellValue(bd.GetValue("singerName").ToString());
rowTmp.CreateCell().SetCellValue(string.IsNullOrEmpty(bd.GetValue("errorReason").ToString()) ? "" : bd.GetValue("errorReason").ToString());
rowIndex++;
}
}
}
// 4.生成文件
FileStream file = new FileStream(filePath, FileMode.Create);
workbook.Write(file); file.Close();
Response.AppendHeader("Content-Disposition", "attachment;filename=ErrorSongIds.xls");
Response.ContentType = "application/ms-excel";
Response.WriteFile(filePath);
Response.Flush();
Response.End();
}
catch (Exception ex) { throw ex; }
}
由于没有找到哪里上传文件,需要NPOI.dll文件的给我留言哈,见谅咯。
附加动态加载Excel列和合并单元格的代码:
public void CreateExport()
{
try
{string fileName = "Members.xls"; // 文件名称
string filePath = Path.Combine(HttpRuntime.AppDomainAppPath, "Template") + "/" + fileName;
// 1.检测是否存在文件夹,若不存在就建立个文件夹
string directoryName = Path.GetDirectoryName(filePath);
if (!Directory.Exists(directoryName))
{
Directory.CreateDirectory(directoryName);
}
// 2.解析单元格头部,设置单元头的中文名称
HSSFWorkbook workbook = new HSSFWorkbook(); // 工作簿
ISheet sheet = workbook.CreateSheet("会员列表"); #region 设置Excel表格第一行的样式
IRow titleInfo = sheet.CreateRow();
ICell cellTitle = titleInfo.CreateCell();
cellTitle.SetCellValue("会员信息批量录入模板");
ICellStyle titleStyle = workbook.CreateCellStyle();
IFont titleFont = workbook.CreateFont();
titleFont.FontHeightInPoints = ;
titleFont.Boldweight = short.MaxValue;//字体加粗
titleStyle.SetFont(titleFont);
cellTitle.CellStyle = titleStyle; #endregion IRow dataFields = sheet.CreateRow(); ICellStyle style = workbook.CreateCellStyle();//创建样式对象
style.Alignment = HorizontalAlignment.CENTER;//水平对齐
style.VerticalAlignment = VerticalAlignment.CENTER;//垂直对齐
IFont font = workbook.CreateFont(); //创建一个字体样式对象
font.FontName = "宋体"; //和excel里面的字体对应
font.Color = new HSSFColor.RED().GetIndex();//颜色参考NPOI的颜色对照表(替换掉PINK())
font.FontHeightInPoints = ;//字体大小
font.Boldweight = short.MaxValue;//字体加粗
style.SetFont(font); //将字体样式赋给样式对象
sheet.SetColumnWidth(, * );//设置列宽 string[] colums = { "*会员卡号", "*会员手机", "*会员姓名", "*会员等级", "会员性别", "电子邮箱", "会员状态", "固定电话", "永久有效", "身份证号", "开卡费用", "会员地址", "备注信息" };
ICell Cell = null;
for (int i = ; i < colums.Count(); i++)
{
Cell = dataFields.CreateCell(i);
Cell.CellStyle = style;
Cell.SetCellValue(colums[i]);
sheet.SetColumnWidth(i, * );
}
sheet.AddMergedRegion(new CellRangeAddress(, , , colums.Count()));//合并单元格
// 4.生成文件
FileStream file = new FileStream(filePath, FileMode.Create);
workbook.Write(file); file.Close();
Response.AppendHeader("Content-Disposition", "attachment;filename=Members.xls");
Response.ContentType = "application/ms-excel";
Response.WriteFile(filePath);
Response.Flush();
Response.End();
}
catch (Exception ex) { throw ex; }
}
C#生成Excel保存到服务器端并下载的更多相关文章
- npoi生成excel流并在客户端下载(html+后台 )
//前端页面 <body> <input type="button" value="导出Excel" class="button&q ...
- JXL生成Excel,并提供下载(2:提供下载)
实现效果: 项目中使用JXL生成Excel,使生成的Excel可以让用户下载,如下图 一.生成Excel 二.提供下载 实现思路: 1.页面使用form表单提交方式(Ajax提交方式,我这里不行) 2 ...
- 通过Workbook类 生成Excel导出数据
需求: 实现错误信息生成Excel保存到本地让用户查看. 刚开始使用了微软自带的Microsoft.Office.Interop.Excel类库. Microsoft.Office.Interop.E ...
- asp.net 生成 excel导出保存时, 解决迅雷下载aspx页面问题
网络上搜索,一大堆废话,以下为简单的导出生成Excel代码: string excelFile = Server.MapPath("~/SB/UpFile/20151104111008/Bo ...
- Java上传下载excel、解析Excel、生成Excel
在软件开发过程中难免需要批量上传与下载,生成报表保存也是常有之事,最近集团门户开发用到了Excel模版下载,Excel生成,圆满完成,对这一知识点进行整理,资源共享,有不足之处还望批评指正,文章结尾提 ...
- java动态生成excel打包下载
@SuppressWarnings("unchecked") public String batchExport() throws DBException{ @SuppressWa ...
- .net生成Excel,并下载
生成Excel的方式有很多种,这里记录两个最简单的: 1.将数据保存为html,然后输出到客户端,保存为Excel文件: 2.通过\t\n生成字符串,然后输出到客户端,保存为Excel. 以上两者的原 ...
- Asp.net MVC 简单实现生成Excel并下载
由于项目上的需求,需要导出指定条件的Excel文件.经过一翻折腾终于实现了. 现在把代码贴出来分享 (直接把我们项目里面的一部份辅助类的代码分享一下) 我们项目使用的是Asp.Net MVC4.0模式 ...
- 使用node.js生成excel报表下载(excel-export express篇)
引言:日常工作中已经有许多应用功能块使用了nodejs作为web服务器,而生成报表下载也是我们在传统应用. java中提供了2套类库实现(jxl 和POI),.NET 作为微软的亲儿子更加不用说,各种 ...
随机推荐
- adb server version (31) doesn't match this client (40); killing...
删除360的手机助手即可解决,进程名字360MoblieMgr.exe
- ext Ext.grid.去除右边空白
1.当Scroll没有显示时,Ext.grid右边会显示一个空白间隔. 2.解决办法<View> <ext:GridView ForceFit="true" Sc ...
- 前端分辨pc和移动端导入不同css
通过navigator获取浏览器,根据浏览器的不同判断出pc和移动端然后设置不同的css 分辨不同屏幕导入不同的css文件: function loadCSS() { if((navigator.us ...
- JMeter(三)遇到的问题01: 通过CSV Data Set Config参数化有中文时,显示为?
当使用CSV Data Set Config进行参数化,内容中含有中文时,响应文本显示为? 解决办法:只需要将“file encoding”设置为“gb2312”就可以了.
- window 安装gcc交叉编译器
参考网址: https://blog.csdn.net/zsy19881226/article/details/46952535
- 列式数据库~clickhouse日常管理
clickhouse日常管理一 变量相关 1 查看变量 system.setting相关表 2 设置变量 set variables= 请注意这里是session级别,如果想永久生 ...
- bash常用指令(未完)
#号注释1.man 2.cd 3.clear 4.alias 命令别名设置 区别命令别名和变量的区别,别名可以直接执行,本质是一个函数的索引 5.grep 查找字符串 grep [-acinv] [- ...
- APACHE - CXF 入门详解
ref: https://www.cnblogs.com/hoojjack/p/6724659.html
- NMON使用以及nmon_analyse生成分析报表
在我们监控我们的操作系统的时候如果可以把各个硬件的监控信息生成形象化的分析报表图对于我们来说是件太好的事情了,而通过ibm的nom和nmon_analyser两者的结合完全可以实现我们的要求.首先对n ...
- nginx反向代理解决跨域
nginx作为反向代理服务器,就是把http请求转发到另一个或者一些服务器上.通过把本地一个url前缀映射到要跨域访问的web服务器上,就可以实现跨域访问.对于浏览器来说,访问的就是同源服务器上的一个 ...