asp.net 导出Excel方法汇总
第一种:需要引用com :microsoft.excel.11.0.
//生成Excel文件的代码
protected void ExportExcel()
{
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook wb = excel.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet); // 创建工作簿
Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[1]; // 创建工作页
DataSet ds = GetData();
int iMaxRow = ds.Tables["rsData"].Rows.Count;
int iMaxCol = ds.Tables["rsData"].Columns.Count;
// 设置格式
ws.get_Range(ws.Cells[1, 1], ws.Cells[1, iMaxCol]).Font.Name = "黑体";
ws.get_Range(ws.Cells[1, 1], ws.Cells[1, iMaxCol]).Font.Bold = true;
ws.get_Range(ws.Cells[1, 1], ws.Cells[iMaxRow + 1, iMaxCol]).Borders.LineStyle = 1;
// 设置标题
excel.Cells[1, 1] = "班级";
excel.Cells[1, 2] = "学号";
excel.Cells[1, 3] = "姓名";
excel.Cells[1, 4] = "性别";
// 填充数据
for (int iRow = 0; iRow < iMaxRow; iRow++)
{
for (int iCol = 0; iCol < iMaxCol; iCol++)
{
excel.Cells[iRow + 2, iCol + 1] = ds.Tables["rsData"].Rows[iRow][iCol].ToString();
}
}
// 保存Excel
excel.Save("学生基础信息");
// 打开Excel
excel.Visible = true;
}
第二种:利用 System.IO
public void CreateExcel(DataSet ds, string typeid, string FileName)
{
HttpResponse resp;
resp = Page.Response;
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.ContentType = "application/ms-excel";
resp.AddHeader("Content-Dispositi
相关文档:
1. 打开新的窗口并传送参数:
传送参数:
response.write("<script>window.open('*.aspx?id="+this.DropDownList1.SelectIndex+"&id1="+...+"')</script>")
接收参数:
string a = Request.QueryString("id");
string b = Request.QueryString("id1");
2.为按钮添加对话框
Button1.Attributes.Add("o ......
c#(或vb.net)程序改进
1、使用值类型的ToString方法
在连接字符串时,经常使用"+"号直接将数字添加到字符串中。这种方法虽然简单,也可以得到正确结果,但是由于涉及到不同的数据类型,数字需要通过装箱操作转化为引用类型才可以添加到字符串中。但是装箱操作对性能影响较大,因为在进行这类处理时,将在托管堆中 ......
要想了解asp.net 2.0的异步页的处理过程,先列出页面的生命周期:
1 :Init 事件: 页面初始化 ,初始化设置。
2: LoadViewState方法: 加载视图状态, 填充ViewState属性。
3 :LoadPostData方法: 处理回发数据, 处理传入窗体数据。
4: Load 事件: 加载页面 ,页面控件初始化完成并反映了客户端的数据。
5 :Ra ......
数字/字母混合很简单的,看着比较舒服,前台生成的aspx文件我就不贴出来了,默认的,我也未作修改。下面只贴出后台的cs代码。仅供参考。
public partial class CheckCode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.CreateCheckCodeImage( ......
JSON Serialization and Deserialization in ASP.Net
I was looking around for a simple example which would just do an object serialization to a JSON format, and then deserializing back to the original object. I found few examples on MSDN, but did seem to be too long ......