首先定义一个实体类
/// <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(); }