码迷,mamicode.com
首页 > Windows程序 > 详细

C#生成Excel保存到服务器端并下载

时间:2017-05-12 20:27:04      阅读:354      评论:0      收藏:0      [点我收藏+]

标签:for   try   document   服务   pre   error   mod   append   request   

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(0); row.CreateCell(0).SetCellValue("原始ID");
                row.CreateCell(1).SetCellValue("歌曲名称");
                row.CreateCell(2).SetCellValue("歌手名");
                row.CreateCell(3).SetCellValue("失败原因");
                //_songListService.getErrorExcel(uid);
                BsonArray array = null;
                int rowIndex = 1; BsonDocument bd = null;
                if (array != null && array.Count > 0)
                {
                    for (int i = 0; i < array.Count; i++)
                    {
                        IRow rowTmp = sheet.CreateRow(rowIndex);
                        bd = (BsonDocument)array[i];
                        if (bd != null)
                        {
                            rowTmp.CreateCell(0).SetCellValue(bd.GetValue("sourceId").ToString());
                            rowTmp.CreateCell(1).SetCellValue(bd.GetValue("songName").ToString());
                            rowTmp.CreateCell(2).SetCellValue(bd.GetValue("singerName").ToString());
                            rowTmp.CreateCell(3).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; }
        }

 

C#生成Excel保存到服务器端并下载

标签:for   try   document   服务   pre   error   mod   append   request   

原文地址:http://www.cnblogs.com/LoveQin/p/6846839.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!