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保存到服务器端并下载的更多相关文章

  1. npoi生成excel流并在客户端下载(html+后台 )

    //前端页面 <body> <input type="button" value="导出Excel" class="button&q ...

  2. JXL生成Excel,并提供下载(2:提供下载)

    实现效果: 项目中使用JXL生成Excel,使生成的Excel可以让用户下载,如下图 一.生成Excel 二.提供下载 实现思路: 1.页面使用form表单提交方式(Ajax提交方式,我这里不行) 2 ...

  3. 通过Workbook类 生成Excel导出数据

    需求: 实现错误信息生成Excel保存到本地让用户查看. 刚开始使用了微软自带的Microsoft.Office.Interop.Excel类库. Microsoft.Office.Interop.E ...

  4. asp.net 生成 excel导出保存时, 解决迅雷下载aspx页面问题

    网络上搜索,一大堆废话,以下为简单的导出生成Excel代码: string excelFile = Server.MapPath("~/SB/UpFile/20151104111008/Bo ...

  5. Java上传下载excel、解析Excel、生成Excel

    在软件开发过程中难免需要批量上传与下载,生成报表保存也是常有之事,最近集团门户开发用到了Excel模版下载,Excel生成,圆满完成,对这一知识点进行整理,资源共享,有不足之处还望批评指正,文章结尾提 ...

  6. java动态生成excel打包下载

    @SuppressWarnings("unchecked") public String batchExport() throws DBException{ @SuppressWa ...

  7. .net生成Excel,并下载

    生成Excel的方式有很多种,这里记录两个最简单的: 1.将数据保存为html,然后输出到客户端,保存为Excel文件: 2.通过\t\n生成字符串,然后输出到客户端,保存为Excel. 以上两者的原 ...

  8. Asp.net MVC 简单实现生成Excel并下载

    由于项目上的需求,需要导出指定条件的Excel文件.经过一翻折腾终于实现了. 现在把代码贴出来分享 (直接把我们项目里面的一部份辅助类的代码分享一下) 我们项目使用的是Asp.Net MVC4.0模式 ...

  9. 使用node.js生成excel报表下载(excel-export express篇)

    引言:日常工作中已经有许多应用功能块使用了nodejs作为web服务器,而生成报表下载也是我们在传统应用. java中提供了2套类库实现(jxl 和POI),.NET 作为微软的亲儿子更加不用说,各种 ...

随机推荐

  1. js中文编码到C#后台解码

    escape() 方法: 采用ISO Latin字符集对指定的字符串进行编码.所有的空格符.标点符号.特殊字符以及其他非ASCII字符都将被转化成%xx格式的字符编码(xx等于该字符在字符集表里面的编 ...

  2. 学习 vue 源码 -- 响应式原理

    概述 由于刚开始学习 vue 源码,而且水平有限,有理解或表述的不对的地方,还请不吝指教. vue 主要通过 Watcher.Dep 和 Observer 三个类来实现响应式视图.另外还有一个 sch ...

  3. ****** 二十八 ******、软设笔记【数据库】-分布式数据库、特点、数据存储、DBMS组成

    分布式数据库    一.分布式数据库        分布式数据库由一组数据组成,这些数据物理上分布在计算机网络的不同结点(场地)上,逻辑上是属于同一个系统.每个结点可以执行局部应用,也能通过网络通信子 ...

  4. tensorflow can not find libcusolver.so.8.0

    ImportError: libcusolver.so.8.0: cannot open shared object file: No such file or directory solution: ...

  5. 区分defer和async

    今天要介绍的让脚本延迟加载,让脚本延迟加载的方式有多种,最简单粗暴的方法就是将 <script> 标签放在 <body> 标签的最下面,这样就可以按照先后顺序依次执行了,但是你 ...

  6. Linux命令之-ps & kill

    1.ps:将某个进程显示出来: 常用命令 :ps -ef |grep Java 1)如下为加不加-e参数的区别 2.一般我们查找某个进程的目的就是把它杀掉,使用kill 命令. kill -9 564 ...

  7. java Object类的公共方法

    1.HashCode();      2. wait();  3. notify(); 4.equals(); 5.getClass(); 6.toString(); 7.clone(); 8.fin ...

  8. linux把文件压缩成.tar.gz的命令

    https://blog.csdn.net/qq_27803491/article/details/52785838

  9. json数据交互

    springmvc 的json数据交互 - 哎幽的成长 - CSDN博客--和老师讲课相同http://blog.csdn.net/u012373815/article/details/4720818 ...

  10. 二叉查找树(Binary Search Tree)

    二叉树的一个重要的应用是他们在查找中的使用. 以下是二叉查找树的查找代码 #include <stdio.h> int main() { typedef struct Node{ int ...