在C#中使用.net3.0和Openxml在服务器端实现数据格式转换成OpenxmlExcel

本文介绍如何在C#中利用.NET3.0和OpenXML库将数据表数据转换为Excel文件供用户下载。通过安装OpenXML.dll、创建模板或直接生成文件,然后提供具体的转换代码,最后通过流方式提供给用户下载。代码示例包括创建内存中的Excel文件,插入数据,并设置样式。
摘要由CSDN通过智能技术生成

日前公司要求实现将data table中的数据转换成Excel文件格式提供给用户下载,几经周折,试过很多中方法,终于找到一个相对比较好的解决方案,虽然不够漂亮,但是基本上实现了这个需求。
1 。安装。Net3.0 ,下载 Openxml.dll
这些都是可以从微软的网站上免费获得。

2。可以做使用Excel2007做一个模板,设置好style,这样可以很方便。如果自己生成新的Excel文件的话,也可以,不过要深入研究OpenXml的格式。
3。具体的转换代码(需要改动
4。在具体的使用中,使用stream的方式提供给用户下载。代码如下:
(1)调用代码
 DataTable dt = .........

            string parentFolderPath = Server.MapPath("Excel");
            if (dt == null) return;
            ORE.BusinessLogic.TransferToExcel transfer = new ORE.BusinessLogic.TransferToExcel();
            byte[] results = transfer.TransferToOpenXMLExcelByStream(parentFolderPath, dt, ViewState["Option"].ToString());
            if (results != null)
            {
                Response.ClearContent();
                Response.ContentType = "application/x-zip-compressed";  //this is very important
                Response.ContentEncoding = System.Text.Encoding.UTF8;
                Response.AddHeader("Content-Disposition", "attachment;filename=" + LabelTitle.Text.Replace(" ", "") + ".xlsx");
                Response.BinaryWrite(results);
                Response.Flush();
                Response.Close();
            }
            else
            {
                string errorMessage = "Transfer process terminated unexpectedly, please contact the ORE Helpdesk";
                Response.Write("<script language='javascript'>alert('" + errorMessage + "');</script>");
            }
(2) transferring code

using System;
using System.Data;
using System.Configuration;
using System.Web;
using Microsoft.Office.DocumentFormat.OpenXml.Packaging;
using System.IO.Packaging;
using System.IO;
using System.Data.SqlClient;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.Text;


    /// <summary>
    /// Transfer the data to the OpenXML Excel format
    /// Added by zhailei 2007-12-24
    /// needs to add WindowsBase and OpenXml references
    /// </summary>
    struct OutputColumn
    {
        private string _colName;
        private string _outputColName;
        private int _outputColWidth;
        public string ColumnName
        {
            get
            {
                return _colName;
            }
        }
        public string OutputColumnName
        {
            get
            {
                return _outputColName;
            }
        }
        public int OutputColWidth
        {
            get
            {
                return _outputColWidth;
            }
        }
        public OutputColumn(string colName, string outputColName):this(colName,outputColName,10)
        {
           
        }
        public OutputColumn(string colName, string outputColName, int outputColWidth)
        {
            this._colName = colName;
            this._outputColName = outputColName;
            this._outputColWidth = outputColWidth;
        }
    }
    public class TransferToExcel
    {
        public byte[] TransferToOpenXMLExcelByStream(string parentFolderPath, DataTable data,string option)
        {
            //template file
            string filePath = parentFolderPath + @"/template.xlsx";
            FileInfo file = new FileInfo(filePath);
            if (!file.Exists)
            {
                return null;
            }
            //read the template file to a byte[] buffer
            byte[] srcFileBuffer;
            using (FileStream srcFileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                srcFileBuffer = new byte[srcFileStream.Length];
                srcFileStream.Read(srcFileBuffer, 0, (int)srcFileStream.Length);
            }
            byte[] resultFileBuffer;
            using (MemoryStream

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值