asp.net将sql数据导出excel表格(代sql字段名)
protected void Button1_Click(object sender, EventArgs e)
{
string strsql = "select * from TreeViewTemp where parentID=0";
DataTable dt = db.GetDataTable(strsql);
StringWriter sw = new StringWriter();
//StreamWriter sw = new StreamWriter("d:\\test.xls",false,System.Text.Encoding.Default); 本地保存
ExportExcel(dt, sw);
Response.AddHeader("Content-Disposition", "attachment; filename=PDSDATA.xls");//遠程下載
Response.ContentType = "application/ms-excel";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.Write(sw);
Response.End();
}
public void ExportExcel(DataTable dt, StringWriter w)
{
try
{
for (int i = 0; i < dt.Columns.Count; i++)
{
w.Write(dt.Columns[i]);
w.Write('\t');
}
w.Write("\r\n");
object[] values = new object[dt.Columns.Count];
foreach (DataRow dr in dt.Rows)
{
values = dr.ItemArray;
for (int i = 0; i < dt.Columns.Count; i++)
{
w.Write(values[i]);
w.Write('\t'); //修改语句
}
w.Write("\r\n"); //修改语句
}
w.Flush();
w.Close();
}
catch
{
w.Close();
}
}
引用空间为
using System.IO;
相关文档:
最近刚学会在VB2008 中使用参数化SQL语句,于是马上用到代码中,却碰到查不到任何数据的情况,纠结了好几天,还是没有搞明白,差点吐血。不得已还是先在代码中使用字符串拼接的SQL语句。
包含参数化SQL语句的代码如下:
Dim cmSl As N ......
很多时候我们可能都需要这么一个简繁互相转换的SQL函数,今天在网上找到的,收集下来。
以后有了它就省事多啦。不用再写程序取出来转换后再更新数据库了。
SQL简体繁体转换函数代码:
--生成码表
if exists (select * from dbo.sysobjects where id = object_id(N'[codetable]') and OBJECTPROPERTY(id, N'IsUserTable' ......
BOOL StartServer()
{
if(FAILED(CoInitialize(NULL)))
{
AfxMessageBox(_T("Com初始化失败"));
return FALSE;
}
_SQLServerPtr spSQLServer ......
asp.net中自带的CutString只能截取字符数量的长度,但中英文字符数有差异,一个中文字等同于二个英文字符的宽度,这样对截取后的效果不理想.使用以下的方法就能解决.
//调用方法
string title=BLL.CutStr.CutString("标题",10);using System;
using System.Collections.Generic;
using System.Text;
namespace BLL
{
&n ......