C#常见数据格式导出

首先定义一个实体类

/// <summary>
/// 用户实体类
/// </summary>
public class User
{
    /// <summary>
    /// 编号
    /// </summary>
    public int UserId { get; set; }
    /// <summary>
    /// 姓名
    /// </summary>
    public string UserName { get; set; }
    /// <summary>
    /// 邮箱
    /// </summary>
    public string Email { get; set; }
    /// <summary>
    /// 手机
    /// </summary>
    public string Phone { get; set; }
}

然后初始化一个List<User>

  private List<User> userList = new List<User>
    {
        new User{UserId = 1, UserName="Johh",Email="123456@qq.com",Phone="12345678901"},
        new User{UserId = 2, UserName="Mark",Email="123456@qq.com",Phone="12345678901"},
        new User{UserId = 3, UserName="July",Email="123456@qq.com",Phone="12345678901"},
        new User{UserId = 4, UserName="Shark",Email="123456@qq.com",Phone="12345678901"}
    };

导出Text格式

  protected void ExportText(string fileName)
  {
        StringBuilder sb = new StringBuilder();
        //添加列名
        sb.Append("编号\t");
        sb.Append("姓名\t");
        sb.Append("邮箱\t");
        sb.Append("手机\t");
        sb.Append(Environment.NewLine);
        //循环添加行
        foreach (User user in userList)
        {
            sb.Append(string.Format("{0}\t", user.UserId));
            sb.Append(string.Format("{0}\t", user.UserName));
            sb.Append(string.Format("{0}\t", user.Email));
            sb.Append(string.Format("{0}\t", user.Phone));
            sb.Append(Environment.NewLine);
        }
        //定义Text编码格式
        Encoding myEncoding = Encoding.GetEncoding("GBK");
        //把内容转换成字节序列
        byte[] byteContent = myEncoding.GetBytes(sb.ToString());
        //定义一个内存流
        MemoryStream stream = new MemoryStream(byteContent);
        byte[] byteStream = new byte[stream.Length];
        stream.Seek(0, SeekOrigin.Begin);
        //从当前流中读取内容写入缓存
        stream.Read(byteStream, 0, Convert.ToInt32(byteStream.Length));
        //清空缓存区流的内容
        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlDecode(fileName) + ".txt");
        Response.AddHeader("Content-Length", byteStream.Length.ToString());
        //定义内容类型为二进制
        Response.ContentType = "application/octet-stream";
        //关闭流并释放资源
        Response.Filter.Close();
        //向客户端写入内容
        Response.OutputStream.Write(byteStream, 0, byteStream.Length);
        //向客户端发送当前缓存的输出
        Response.Flush();
        //关闭连接
        Response.Close();
        //释放MemoryStream
        stream.Dispose();
  }

导出excel格式,先引用外部组件aspose.cells.dll

protected void ExportExcel(string fileName)
    {
        //定义一个Excel
        Workbook workbook = new Workbook();
        //定义Excel里面的一页表单
        Worksheet worksheet = workbook.Worksheets[0];
        worksheet.Name = "用户列表";
        //往worksheet添加数据
        worksheet.Cells.ImportCustomObjects(userList, null, true, 0, 0, userList.Count, true, "yyyy-MM-dd hh:mm:ss", false);
        int i = 0;
        //循环替换列名
        foreach (Cell cell in worksheet.Cells)
        {
            string cellValue = worksheet.Cells[0, i].Value.ToString();
            switch (cellValue)
            {
                case "UserId":
                    cellValue = "编号";
                    break;
                case "UserName":
                    cellValue = "姓名";
                    break;
                case "Email":
                    cellValue = "邮箱";
                    break;
                case "Phone":
                    cellValue = "手机";
                    break;
                default:
                    break;
            }
        }
        //冻结第一行
        worksheet.FreezePanes(1, 1, 1, 0);
        //自动设置列宽
        worksheet.AutoFitColumns();
        //定义一个内存流
        MemoryStream stream = new MemoryStream();
        //保存workbook到流中并定义格式
        workbook.Save(stream, SaveFormat.Excel97To2003);
        //获取字节序列
        byte[] byteStream = stream.GetBuffer();
        //清空缓存区流的内容
        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlDecode(fileName) + ".xls");
        Response.AddHeader("Content-Length", byteStream.Length.ToString());
        //定义内容类型为二进制
        Response.ContentType = "application/octet-stream";
        //写入内容
        Response.OutputStream.Write(byteStream, 0, byteStream.Length);
        //关闭连接
        Response.End();
    }

 

转载于:https://www.cnblogs.com/qinming/p/4000195.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值