All articles(网络文学目录) All Pictures(图片目录) All Softwares(软件目录)

 
TABLE导入到EXCEL_[Asp.Net教程]

Writer: 归海一刀 Article type: Programming skills(编程技巧) Time: 2014/1/30 1:29:57 Browse times: 350 Comment times: 0

TABLE导入到EXCEL_[Asp.Net教程]


Head photo

Go homepage
Upload pictures
Write articles

TABLE导入到EXCEL_[Asp.Net教程] 前台代码:ExportExcel1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExportExcel1.aspx.cs" Inherits="ExportExcel1" %>





导出数据到EXCEL


Table Example, constructed programmatically



GridLines="Both"
HorizontalAlign="Center"
Font-Name="Verdana"
Font-Size="8pt"
CellPadding=15
CellSpacing=0
Runat="server"/>




后台代码:ExportExcel1.aspx .cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class ExportExcel1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Generate rows and cells.
TableRow r = new TableRow();
TableCell c1 = new TableCell();
c1.ColumnSpan = 2;
c1.Text = "test";
c1.HorizontalAlign = HorizontalAlign.Center;
r.Cells.Add(c1);
Table1.Rows.Add(r);
int numrows = 3;
int numcells = 2;
for (int j=0; j {
TableRow r1 = new TableRow();
for (int i=0; i {
TableCell c = new TableCell();
c.Controls.Add(new LiteralControl("row " + j.ToString() + ", cell " + i.ToString()));
r1.Cells.Add(c);
}
Table1.Rows.Add(r1);
}
}
protected void Button1_Click(object sender, EventArgs e)
{

DateTime dt = System.DateTime.Now;//取出当前系统日期时间
string dtt = dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString();//取出系统日期
string filestr = "C:\\excel"; //filestr是文件的路径
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
Table1.RenderControl(htmlWriter);
string file = filestr + "\\" + dtt + ".xls";
if (!Directory.Exists(filestr))
{
Directory.CreateDirectory(filestr);
}
System.IO.StreamWriter sw = new StreamWriter(file);
sw.Write(stringWriter.ToString());
sw.Close();

}
}




There are 0 records,
Comment:
Must be registered users to comment(必须是注册用户才能发表评论)

Disclaimer Privacy Policy About us Site Map
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.