c#(asp.net) excel操作实例
本文导语: c# excel操作实例,实现如下的功能: 将datatable中的数据导出到指定的excel文件中;将指定excel文件中的数据转换成datatable对象,供应用程序进一步处理;将指定html字符串的数据转换成datatable对象 --根据“”等特殊字符进行处理...
c# excel操作实例,实现如下的功能:
将datatable中的数据导出到指定的excel文件中;将指定excel文件中的数据转换成datatable对象,供应用程序进一步处理;将指定html字符串的数据转换成datatable对象 --根据“”等特殊字符进行处理;从指定html字符串中剔除指定的对象等。
丰富而全面,是学习使用c#(asp.net)操作数据与excel相互转换的好例子。
///
/// 将datatable中的数据导出到指定的excel文件中
/// http://www.
/// 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(""))
{
tmpstri=tmpstri.replace("","^");
string[] tmpfield=tmpstri.split(separator);
for(int j=0;j0)
{
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("