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 作为微软的亲儿子更加不用说,各种 ...
随机推荐
- 从零开始学HTTP (一)网络基础
网络基础 web发展史 下面列出了web发展中几个重要的历史结点 1990年 HTTP/0.9问世(HTTP/0.9含有HTTP1.0之前版本的意思,这时HTTP并未作为标准被公布) CERN(欧洲核 ...
- CC2541设置中断输入模式
//P0.0 /* SW_6 is at P0.1 */#define HAL_KEY_SW_6_PORT P0#define HAL_KEY_SW_6_BIT BV(0)#define HAL_KE ...
- hadoop1.2.1的安装
前提:1.机器最好都做ssh免密登录,最后在启动hadoop的时候会简单很多 免密登录看免密登录 2.集群中的虚拟机最好都关闭防火墙,否则很麻烦 3集群中的虚拟机中必须安装jdk. 具体安装步骤如下: ...
- Express 体验 路由、模板引擎、中间件
http://expressjs.com/en/4x/api.html#req.method http://expressjs.com/en/guide/routing.html [Route pat ...
- sql -leetcode 178. Rank Scores
Score 很好得到: select Score from Scores order by Score desc; 要得到rank, 可以通过比较比当前Score 大的Score 的个数得到: sel ...
- 执行sql,使用带参的写法
db.ExecuteNonQuery("UPDATE QU_QUALITYREPORT SET RETOSTATUS=1 WHERE BATCHID=@0 AND PROVINCEAREA ...
- linux 删除占用文件
清空 程序占用日志文件: cat /dev/null > log 或: echo " " > log 直接删除程序占用文件大小不生效: rm -rf log ps -e ...
- 【vue】中 provide 和 inject 的使用方法
<div id="app"> hello <my-button> </my-button> </div> <script sr ...
- Python的集合和元组
一.元组 元组也是一个list,但它的值不能改变 Python 的元组与列表类似,不同之处在于元组的元素不能修改. 元组使用小括号,列表使用方括号. 元组创建很简单,只需要在括号中添加元素,并使用逗号 ...
- 第一章 Android系统的编译和移植实例
第一章 Android系统的编译和移植实例 这一章节主要介绍了Android系统的编译和移植技术,作为建立在Linux内核的基础上的Android操作系统,它的编译和移植不论在过程还是技术方面都和嵌入 ...