我有一个清单.我想将该列表项转换为xlsx文件的列标题. [英] I have a List. I want to convert that list items to column header of xlsx file.

查看:67
本文介绍了我有一个清单.我想将该列表项转换为xlsx文件的列标题.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Linkbutton.It调用Read类..该类在控制台应用程序中写入..

当用户单击链接按钮时,将出现文件下载"对话框,如果用户单击确定,则打开xlsx(Template.xlsx)文件,并且其列标题为列表项.

我的代码:


I have a Linkbutton.It call the Read Class..that class writen in console application..

When User click a link button then "File Download" dialog box will appear, if user click ok then xlsx(Template.xlsx) file open and it have column headers are list items.

My Code:


Read.cs(Console Application)
{
List<string> list = new List<string>();

            list.Add("Name");
            list.Add("Designation");
            list.Add("Dob");
            list.Add("Address");
            Response.AddHeader("content-disposition", "attachment;filename=Template.xlsx");
            Response.Charset = "";
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
}





但是此内容类型不打开xlsx文件,这会引发错误....我想将列标题设置为Name,Designation,Dob,Address..Pls help


在此先感谢





But this content dype does not open the xlsx file It raise the error....and I want to set the column header as Name,Designation,Dob,Address..Pls help


Thanks in advance

推荐答案

我自己解决了这个问题...

我的代码:

I have solved this myself...

My code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OfficeOpenXml;


namespace Downloadxlsx
{
    public partial class ReadExcel : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void lkl_Click(object sender, EventArgs e)
        {
            using (ExcelPackage pck = new ExcelPackage())
            {

                
                List<string> list = new List<string>();

                list.Add("Name");
                list.Add("Designation");
                list.Add("Dob");
                list.Add("Address");

                ExcelWorksheet ws = pck.Workbook.Worksheets.Add("SearchReport");
                int col = 1;
                for (int i = 0; i < list.Count; i++) // Loop through List with for  
                {

                    ws.Cells[1, col].Value = list[i].ToString();
                    col++;

                }

                Byte[] fileBytes = pck.GetAsByteArray();
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.Cookies.Clear();
                              Response.Cache.SetCacheability(HttpCacheability.Private);
                Response.CacheControl = "private";
                Response.Charset = System.Text.UTF8Encoding.UTF8.WebName;
                Response.ContentEncoding = System.Text.UTF8Encoding.UTF8;
                Response.AppendHeader("Content-Length", fileBytes.Length.ToString());
                Response.AppendHeader("Pragma", "cache");
                Response.AppendHeader("Expires", "60");
                Response.AppendHeader("Content-Disposition",
                "attachment; " +
                "filename=\"ExcelReport.xlsx\"; " +
                "size=" + fileBytes.Length.ToString() + "; " +
                "creation-date=" + DateTime.Now.ToString("R") + "; " +
                "modification-date=" + DateTime.Now.ToString("R") + "; " +
                "read-date=" + DateTime.Now.ToString("R"));
                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                       Response.BinaryWrite(fileBytes);
                Response.End();
            }
        }
    }
}</string></string>


这篇关于我有一个清单.我想将该列表项转换为xlsx文件的列标题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆