EXCEL表用c#来读
using
System;
using
System.IO;
using
System.Web;
using
System.Web.SessionState;
using
NickLee.Common.ExcelLite;
namespace
excel1 {
///
<summary>
///
excel 的摘要说明。
///
</summary>
public
class
excel {
public
excel() {
//
//
TODO: 在此处添加构造函数逻辑
//
fileName
=
HttpContext.Current.Server.
MapPath(
"
.
"
)
+
"
\\temp\\
"
+
g
+
"
.xls
"
; }
private
Guid g
=
Guid.NewGuid();
private
string
fileName;
private
ExcelFile ec
=
new
ExcelFile();
///
<summary>
///
从model中复制模版到temp中
///
</summary>
///
<param name="modelName">
只需传入模版的名字即可(注意是字符串)
</param>
public
void
copyModel(
string
modelName) {
string
MName
=
HttpContext.
Current.Server.MapPath(
"
.
"
)
+
"
\\model\\
"
+
modelName; File.Copy(MName,fileName); }
///
<summary>
///
设置EXCEL表中的字表个数,字表在这个表中统一命名为“续表”+数字序号
///
</summary>
///
<param name="sheetnum">
设置字表的个数
</param>
public
void
setSheets(
int
sheetnum) { ec.LoadXls(fileName); ExcelWorksheet wsx
=
ec.Worksheets[
0
];
for
(
int
y
=
0
;y
<
sheetnum;y
++
) {
int
num
=
y
+
1
; ec.Worksheets.AddCopy(
"
续表
"
+
num,wsx); } }
///
<summary>
///
给EXCEL表的各个字段设置值
///
</summary>
///
<param name="sheetnum">
表的序号,注意表是以“0”开始的
</param>
///
<param name="x">
对应于EXCEL的最左边的数字列
</param>
///
<param name="y">
对应于EXCEL的最上面的字母列
</param>
///
<param name="values">
需要写入的值
</param>
public
void
setValues(
int
sheetnum,
int
x,
int
y,
string
values) { ec.Worksheets[sheetnum].Cells[x,y].Value
=
values;; }
///
<summary>
///
保存函数,要是文件中有相同的文件就要删除了,
///然后重新写入相同文件名的新EXCEL表
///
</summary>
public
void
saveFile() {
if
(File.Exists(fileName)) { File.Delete(fileName); ec.SaveXls(fileName); }
else
{ ec.SaveXls(fileName); } }
///
<summary>
///
显示的EXCEL表格
///
</summary>
public
void
showFile() { HttpContext.Current.Response.Charset
=
System.Text.Encoding.Default.WebName; HttpContext.Current.Response.ContentType
=
"
application/vnd.ms-excel
"
; HttpContext.Current.Response.ContentEncoding
=
System.Text.Encoding.UTF8; HttpContext.Current.Response.WriteFile(fileName); HttpContext.Current.Response.End(); } } }