zoukankan
html css js c++ java
导出导入Excel(DataSet,DataGrid)
public
class
ExportExcel
{
public
ExportExcel()
{
}
private
static
void
GetXslFile(DataSet ds,
string
xslPath)
{
string
strColumn
=
""
;
string
strRow
=
""
;
string
dsName
=
ds.DataSetName;
string
tableName
=
ds.Tables[
0
].TableName;
string
header
=
dsName
+
"
/
"
+
tableName;
foreach
(DataColumn clm
in
ds.Tables[
0
].Columns)
{
//
特殊字符 <,>,",*,%,(,),& 替换
//
*************************************************
//
*************************************************
//
符号 xml下的值 excel中的值
//
< -------- _x003C_ ------ <
//
> -------- _x003E_ ------ >
//
" -------- _x0022_ ------ "
//
* -------- _x002A_ ------ *
//
% -------- _x0025_ ------ %
//
& -------- _x0026_ ------ &
//
( -------- _x0028_ ------ (
//
) -------- _x0029_ ------ )
//
= -------- _x003D_ ------ =
//
*************************************************
//
*************************************************
string
strClmName
=
clm.ColumnName;
string
strRowName
=
clm.ColumnName;
if
(strClmName.IndexOf(
"
&
"
)
!=-
1
)
{
strClmName
=
strClmName.Replace(
"
&
"
,
"
&
"
);
}
if
(strClmName.IndexOf(
"
<
"
)
!=-
1
)
{
strClmName
=
strClmName.Replace(
"
<
"
,
"
<
"
);
}
if
(strClmName.IndexOf(
"
>
"
)
!=-
1
)
{
strClmName
=
strClmName.Replace(
"
>
"
,
"
>
"
);
}
if
(strClmName.IndexOf(
"
\
""
)!=-1)
{
strClmName
=
strClmName.Replace(
"
\
""
,
"
&
quot;
"
);
}
if
(strRowName.IndexOf(
"
<
"
)
!=-
1
)
{
strRowName
=
strRowName.Replace(
"
<
"
,
"
_x003C_
"
);
}
if
(strRowName.IndexOf(
"
>
"
)
!=-
1
)
{
strRowName
=
strRowName.Replace(
"
>
"
,
"
_x003E_
"
);
}
if
(strRowName.IndexOf(
"
\
""
)!=-1)
{
strRowName
=
strRowName.Replace(
"
\
""
,
"
_x0022_
"
);
}
if
(strRowName.IndexOf(
"
*
"
)
!=-
1
)
{
strRowName
=
strRowName.Replace(
"
*
"
,
"
_x002A_
"
);
}
if
(strRowName.IndexOf(
"
%
"
)
!=-
1
)
{
strRowName
=
strRowName.Replace(
"
%
"
,
"
_x0025_
"
);
}
if
(strRowName.IndexOf(
"
&
"
)
!=-
1
)
{
strRowName
=
strRowName.Replace(
"
&
"
,
"
_x0026_
"
);
}
if
(strRowName.IndexOf(
"
(
"
)
!=-
1
)
{
strRowName
=
strRowName.Replace(
"
(
"
,
"
_x0028_
"
);
}
if
(strRowName.IndexOf(
"
)
"
)
!=-
1
)
{
strRowName
=
strRowName.Replace(
"
)
"
,
"
_x0029_
"
);
}
if
(strRowName.IndexOf(
"
=
"
)
!=-
1
)
{
strRowName
=
strRowName.Replace(
"
=
"
,
"
_x003D_
"
);
}
strColumn
+=
"
<th>
"
+
strClmName
+
"
</th>
"
+
"
\r\n
"
;
strRow
+=
"
<td>
"
+
"
<xsl:value-of select=
"
+
"
\
""
+ strRowName +
"
\
""
+
"
/>
"
+
"
</td>
"
+
"
\r\n
"
;
}
string
str
=
@"
<xsl:stylesheet version=""1.0"" xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"">
<xsl:template match=""/"">
<html xmlns:o=""urn:schemas-microsoft-com:office:office"" xmlns:x=""urn:schemas-microsoft-com:office:excel"" xmlns=""http://www.w3.org/TR/REC-html40"">
<head>
<meta http-equiv=""Content-Type"" content=""text/html;charset=utf-8"" />
<style>
.xl24{mso-style-parent:style0;mso-number-format:""\@"";text-align:right;}
</style>
<xml>
<x:ExcelWorkbook>
<x:ExcelWorksheets>
<x:ExcelWorksheet>
<x:Name>Sheet1</x:Name>
<x:WorksheetOptions>
<x:ProtectContents>False</x:ProtectContents>
<x:ProtectObjects>False</x:ProtectObjects>
<x:ProtectScenarios>False</x:ProtectScenarios>
</x:WorksheetOptions>
</x:ExcelWorksheet>
</x:ExcelWorksheets>
</x:ExcelWorkbook>
</xml>
</head>
<body>
"
;
str
+=
"
\r\n
"
+
@"
<table border=""1"" cellpadding=""0"" cellspacing=""0""><tr>
"
+
"
\r\n
"
;
str
+=
strColumn;
str
+=
@"
</tr><xsl:for-each select=""
"
+
header
+
@"
""><tr>
"
;
str
+=
"
\r\n
"
+
strRow;
str
+=
@"
</tr></xsl:for-each></table></body></html></xsl:template></xsl:stylesheet>
"
;
string
path
=
xslPath;
if
(File.Exists(path))
{
File.Delete(path);
}
FileStream fs
=
File.Create(path);
StreamWriter sw
=
new
StreamWriter(fs);
sw.Write(str);
sw.Close();
fs.Close();
}
private
static
void
GetXmlFile(DataSet ds,
string
xmlFilePath)
{
string
strXml
=
ds.GetXml();
if
(File.Exists(xmlFilePath))
{
File.Delete(xmlFilePath);
}
FileStream fs1
=
File.Create(xmlFilePath);
StreamWriter writer
=
new
StreamWriter(fs1);
writer.Write(strXml);
writer.Close();
fs1.Close();
}
private
static
void
BuildExcel(DataSet ds,
string
path)
{
if
(File.Exists(path))
{
File.Delete(path);
}
string
m_path
=
path.Substring(
0
,path.Length
-
4
);
string
m_fileXml
=
m_path
+
"
.xml
"
;
string
m_fileXsl
=
m_path
+
"
.xsl
"
;
string
m_fileXls
=
m_path
+
"
.xls
"
;
try
{
GetXmlFile(ds,m_fileXml);
GetXslFile(ds,m_fileXsl);
//
Excel changed
XmlDocument doc
=
new
XmlDocument();
doc.Load(m_fileXml);
XslTransform xslt
=
new
XslTransform();
xslt.Load(m_fileXsl);
XmlElement root
=
doc.DocumentElement;
XPathNavigator nav
=
root.CreateNavigator();
XmlTextWriter writer
=
new
XmlTextWriter(m_fileXls,
null
);
xslt.Transform(nav,
null
,writer,
null
);
writer.Close();
File.Delete(m_fileXml);
File.Delete(m_fileXsl);
}
catch
{
throw
;
}
}
public
static
void
ToExcel(System.Web.UI.Control ctl,
string
FileName)
{
HttpContext.Current.Response.Charset
=
"
UTF-8
"
;
HttpContext.Current.Response.ContentEncoding
=
System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType
=
"
application/ms-excel
"
;
HttpContext.Current.Response.AppendHeader(
"
Content-Disposition
"
,
"
attachment;filename=
"
+
""
+
FileName
+
"
.xls
"
);
ctl.Page.EnableViewState
=
false
;
System.IO.StringWriter tw
=
new
System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw
=
new
System.Web.UI.HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
public
static
void
DownloadFile(
string
physicalFilePath)
{
FileStream stream
=
null
;
try
{
stream
=
new
FileStream(physicalFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
int
bufSize
=
(
int
)stream.Length;
byte
[] buf
=
new
byte
[bufSize];
int
bytesRead
=
stream.Read(buf,
0
, bufSize);
HttpContext.Current.Response.ContentType
=
"
application/octet-stream
"
;
HttpContext.Current.Response.AppendHeader(
"
Content-Disposition
"
,
"
attachment;filename=
"
+
System.IO.Path.GetFileName(physicalFilePath));
HttpContext.Current.Response.OutputStream.Write(buf,
0
, bytesRead);
HttpContext.Current.Response.End();
}
finally
{
stream.Close();
}
}
}
查看全文
相关阅读:
Eclipse中项目进行发布到Tomcat中的位置
Tomcat中server.xml文件的配置
Tomcat的安装跟配置
实习第二天(查看项目源代码)
mac、windows、linux版jdk1.8下载
idea搭建简单ssm框架的最详细教程(新)
ssm中mapper注入失败的传奇经历
富文本编辑器handyeditor,上传和预览图片的host地址不一样
nginx配置ssl证书
java中pojo对象首字母大写导致无法赋值问题
原文地址:https://www.cnblogs.com/studio313/p/298958.html
最新文章
nginx 安装第三方模块(lua)并热升级
canal中间件
elastalert基本配置说明
elastalert docker安装
Java回调函数的理解与实现
面试题——基本排序算法
面试题——操作系统
面试题——计算机网络
面试题——存储引擎
面试题——数据库
热门文章
面试题——Java虚拟机
关于Spring中的<context:annotation-config/>配置
Redis事件
Redis事务
Ubuntu系统中中文的设置
js中编码的处理
SVN管理工具的使用(实习第9天)
eclipse软件快捷键的使用
实习第四天(bboss框架学习)
jsp页面中自定义标签的小演示
Copyright © 2011-2022 走看看