///
/// 将datatable中的数据导出到指定的excel文件中
///
/// web页面对象
/// 包含被导出数据的datatable对象
/// excel文件的名称
public static void export(system.web.ui.page page,system.data.datatable tab,string filename)
{
system.web.httpresponse httpresponse = page.response;
system.web.ui.webcontrols.datagrid datagrid=new system.web.ui.webcontrols.datagrid();
datagrid.datasource=tab.defaultview;
datagrid.allowpaging = false;
datagrid.headerstyle.backcolor = system.drawing.color.green;
datagrid.headerstyle.horizontalalign = horizontalalign.center;
datagrid.headerstyle.font.bold = true;
datagrid.databind();
httpresponse.appendheader("content-disposition","attachment;filename="+httputility.urlencode(filename,system.text.encoding.utf8)); //filename="*.xls";
httpresponse.contentencoding=system.text.encoding.getencoding("gb2312");
httpresponse.contenttype ="application/ms-excel";
system.io.stringwriter tw = new system.io.stringwriter() ;
system.web.ui.htmltextwriter hw = new system.web.ui.htmltextwriter (tw);
datagrid.rendercontrol(hw);
string filepath = page.server.mappath("..")+"\files\" +filename;
system.io.streamwriter sw = system.io.file.createtext(filepath);
sw.write(tw.tostring());
sw.close();
downfile(httpresponse,filename,filepath);
httpresponse.end();
}
private static bool downfile(system.web.httpresponse response,string filename,string fullpath)
{
try
{
response.contenttype = "application/octet-stream";
response.appendheader("content-disposition","attachment;filename=" +
httputility.urlencode(filename,system.text.encoding.utf8) + ";charset=gb2312");
system.io.filestream fs= system.io.file.openread(fullpath);
long flen=fs.length;
int size=102400;//每100k同时下载数据
byte[] readdata = new byte[size];//指定缓冲区的大小
if(size>flen)size=convert.toint32(flen);
long fpos=0;
bool isend=false;
while (!isend)
{
if((fpos+size)>flen)
{
size=convert.toint32(flen-fpos);
readdata = new byte[size];
isend=true;
}
fs.read(readdata, 0, size);//读入一个压缩块
response.binarywrite(readdata);
fpos+=size;
}
fs.close();
system.io.file.delete(fullpath);
return true;
}
catch
{
return false;
}
}
///
/// 将指定excel文件中的数据转换成datatable对象,供应用程序进一步处理
///
///
///
public static system.data.datatable import(string filepath)
{
system.data.datatable rs = new system.data.datatable();
bool canopen=false;
oledbconnection conn = new oledbconnection("provider=microsoft.jet.oledb.4.0;"+
"data source=" + filepath + ";" +
"extended properties="excel 8.0;"");
try//尝试数据连接是否可用
{
conn.open();
conn.close();
canopen=true;
}
catch{}
if(canopen)
{
try//如果数据连接可以打开则尝试读入数据
{
oledbcommand myoledbcommand = new oledbcommand("select * from [sheet1$]",conn);
oledbdataadapter mydata = new oledbdataadapter(myoledbcommand);
mydata.fill(rs);
conn.close();
}
catch//如果数据连接可以打开但是读入数据失败,则从文件中提取出工作表的名称,再读入数据
{
string sheetname=getsheetname(filepath);
if(sheetname.length>0)
{
oledbcommand myoledbcommand = new oledbcommand("select * from ["+sheetname+"$]",conn);
oledbdataadapter mydata = new oledbdataadapter(myoledbcommand);
mydata.fill(rs);
conn.close();
}
}
}
else
{
system.io.streamreader tmpstream=file.opentext(filepath);
string tmpstr=tmpstream.readtoend();
tmpstream.close();
rs=getdatatablefromstring(tmpstr);
tmpstr="";
}
return rs;
}
///
/// 将指定html字符串的数据转换成datatable对象 --根据“
///
/// html字符串
///
private static datatable getdatatablefromstring(string tmphtml)
{
string tmpstr=tmphtml;
datatable tb=new datatable();
//先处理一下这个字符串,删除第一个
int index=tmpstr.indexof("
tmpstr=tmpstr.substring(index);
else
return tb;
index=tmpstr.lastindexof("
if(index>-1)
tmpstr=tmpstr.substring(0,index+5);
else
return tb;
bool existssparator=false;
char separator=convert.tochar("^");
//如果原字符串中包含分隔符“^”则先把它替换掉
if(tmpstr.indexof(separator.tostring())>-1)
{
existssparator=true;
tmpstr=tmpstr.replace("^","^$&^");
}
//先根据“”分拆
string[] tmprow=tmpstr.replace("","^").split(separator);
for(int i=0;i
datarow newrow=tb.newrow();
string tmpstri=tmprow[i];
if(tmpstri.indexof("
{
tmpstri=tmpstri.substring(tmpstri.indexof("
{
tmpstri=tmpstri.replace("","^");
string[] tmpfield=tmpstri.split(separator);
for(int j=0;j
tmpfield[j]=removestring(tmpfield[j],"");
index=tmpfield[j].lastindexof(">")+1;
if(index>0)
{
string field=tmpfield[j].substring(index,tmpfield[j].length-index);
if(existssparator) field=field.replace("^$&^","^");
if(i==0)
{
string tmpfieldname=field;
int sn=1;
while(tb.columns.contains(tmpfieldname))
{
tmpfieldname=field+sn.tostring();
sn+=1;
}
tb.columns.add(tmpfieldname);
}
else
{
newrow[j]=field;
}
}//end of if(index>0)
}
if(i>0)
tb.rows.add(newrow);
}
}
}
tb.acceptchanges();
return tb;
}
///
/// 从指定html字符串中剔除指定的对象
///
/// html字符串
/// 需要剔除的对象--例如输入""则剔除""和">"
///
public static string removestring(string tmphtml,string remove)
{
tmphtml=tmphtml.replace(remove.replace("<",""),"");
tmphtml=removestringhead(tmphtml,remove);
return tmphtml;
}
///
/// 只供方法removestring()使用
///
///
private static string removestringhead(string tmphtml,string remove)
{
//为了方便注释,假设输入参数remove=""
if(remove.length<1) return tmphtml;//参数remove为空:不处理返回
if((remove.substring(0,1)!="<")||(remove.substring(remove.length-1)!=">")) return tmphtml;//参数remove不是????>:不处理返回
int indexs=tmphtml.indexof(remove.replace(">",""));//查找“int indexe=-1;
if(indexs>-1)
{
string tmpright=tmphtml.substring(indexs,tmphtml.length-indexs);
indexe=tmpright.indexof(">");
if(indexe>-1)
tmphtml=tmphtml.substring(0,indexs)+tmphtml.substring(indexs+indexe+1);
if(tmphtml.indexof(remove.replace(">",""))>-1)
tmphtml=removestringhead(tmphtml,remove);
}
return tmphtml;
}
///
/// 将指定excel文件中读取第一张工作表的名称
///
///
///
private static string getsheetname(string filepath)
{
string sheetname="";
system.io.filestream tmpstream=file.openread(filepath);
byte[] filebyte=new byte[tmpstream.length];
tmpstream.read(filebyte,0,filebyte.length);
tmpstream.close();
byte[] tmpbyte=new byte[]{convert.tobyte(11),convert.tobyte(0),convert.tobyte(0),convert.tobyte(0),convert.tobyte(0),convert.tobyte(0),convert.tobyte(0),convert.tobyte(0),
convert.tobyte(11),convert.tobyte(0),convert.tobyte(0),convert.tobyte(0),convert.tobyte(0),convert.tobyte(0),convert.tobyte(0),convert.tobyte(0),
convert.tobyte(30),convert.tobyte(16),convert.tobyte(0),convert.tobyte(0)};
int index=getsheetindex(filebyte,tmpbyte);
if(index>-1)
{
index+=16+12;
system.collections.arraylist sheetnamelist=new system.collections.arraylist();
for(int i=index;i
byte temp=filebyte[i];
if(temp!=convert.tobyte(0))
sheetnamelist.add(temp);
else
break;
}
byte[] sheetnamebyte=new byte[sheetnamelist.count];
for(int i=0;i
sheetname=system.text.encoding.default.getstring(sheetnamebyte);
}
return sheetname;
}
///
/// 只供方法getsheetname()使用
///
///
private static int getsheetindex(byte[] findtarget,byte[] finditem)
{
int index=-1;
int finditemlength=finditem.length;
if(finditemlength<1) return -1;
int findtargetlength=findtarget.length;
if((findtargetlength-1)
for(int i=findtargetlength-finditemlength-1;i>-1;i--)
{
system.collections.arraylist tmplist=new system.collections.arraylist();
int find=0;
for(int j=0;j
if(findtarget[i+j]==finditem[j]) find+=1;
}
if(find==finditemlength)
{
index=i;
break;
}
}
return index;
}
asp相关栏目本月热门文章
- 1【Linux驱动开发】设备树详解(二)设备树语法详解
- 2别跟客户扯细节
- 3Springboot+RabbitMQ+ACK机制(生产方确认(全局、局部)、消费方确认)、知识盲区
- 4【Java】对象处理流(ObjectOutputStream和ObjectInputStream)
- 5【分页】常见两种SpringBoot项目中分页技巧
- 6一文带你搞懂OAuth2.0
- 7我要写整个中文互联网界最牛逼的JVM系列教程 | 「JVM与Java体系架构」章节:虚拟机与Java虚拟机介绍
- 8【Spring Cloud】新闻头条微服务项目:FreeMarker模板引擎实现文章静态页面生成
- 9JavaSE - 封装、static成员和内部类
- 10树莓派mjpg-streamer实现监控及拍照功能调试
- 11用c++写一个蓝屏代码
- 12从JDK8源码中看ArrayList和LinkedList的区别
- 13idea 1、报错java: 找不到符号 符号: 变量 log 2、转换成Maven项目
- 14在openwrt使用C语言增加ubus接口(包含C uci操作)
- 15Spring 解决循环依赖
- 16SpringMVC——基于MVC架构的Spring框架
- 17Andy‘s First Dictionary C++ STL set应用
- 18动态内存管理
- 19我的创作纪念日
- 20Docker自定义镜像-Dockerfile
热门相关搜索
路由器设置
木托盘
宝塔面板
儿童python教程
心情低落
朋友圈
vim
双一流学科
专升本
我的学校
日记学校
西点培训学校
汽修学校
情书
化妆学校
塔沟武校
异形模板
西南大学排名
最精辟人生短句
6步教你追回被骗的钱
南昌大学排名
清朝十二帝
北京印刷学院排名
北方工业大学排名
北京航空航天大学排名
首都经济贸易大学排名
中国传媒大学排名
首都师范大学排名
中国地质大学(北京)排名
北京信息科技大学排名
中央民族大学排名
北京舞蹈学院排名
北京电影学院排名
中国戏曲学院排名
河北政法职业学院排名
河北经贸大学排名
天津中德应用技术大学排名
天津医学高等专科学校排名
天津美术学院排名
天津音乐学院排名
天津工业大学排名
北京工业大学耿丹学院排名
北京警察学院排名
天津科技大学排名
北京邮电大学(宏福校区)排名
北京网络职业学院排名
北京大学医学部排名
河北科技大学排名
河北地质大学排名
河北体育学院排名



