-
C# DataTbale
导出
txt
和
xls
1
、
C#
代码类设计
2
、详细代码设计
2.1
导出接口
IExporter
using System;
using c;
using
using
using
namespace
{
public class IExporter
{
protected string
_ExportSaveFileName =
public string ExportSaveFileName
{
get
{
return _ExportSaveFileName;
}
}
public IExporter()
{
}
public virtual void Export(ble dt, bool
isShow = false) { }
protected void OpenFile(string
fileName)
{
Cursor
_currentCursor = t;
t = rsor;
t = _currentCursor;
try
{
s ps = new s();
me
= fileName;
=
Style =
();
}
catch
(Exception ex)
{
throw new Excep
tion((
未能打开该文件,原因
:{0}
< br>()));
}
}
///
///
打开文件
///
public void OpenFile()
{
OpenFile(this._ExportSaveFileName);
}
}
}
public
enum
ExportType
{
[
Description
< br>(
)]
Excel,
[
Description<
/p>
(
+
)]
Txt
}
public
static
class
EnumOperate
{
public
static
string
GetDescriptionFromEnu
mValue(
Enum
value)
{
DescriptionAttribute
attribute = e()
.GetField(ng())
.GetCus
tomAttributes(
typeof
(
DescriptionAttribute
),
false
)
.SingleOrDefault()
as
DescriptionAttribute
;
return
attribute ==
null
? ng() : ption;
}
public
static
T GetEnumValueFromDes
cription
string
description)
{
var
type =
typeof
(T);
if
(!)
throw
new
ArgumentException
();
FieldInfo
[] fields = lds();
var
field = fields
.SelectMany(f => tomAttributes(
typeof
(
Descriptio
nAttribute
),
false
), (
f, a) =>
new
{ Field = f,
Att = a })
.Where(a =>
((
DescriptionAttribute
))
.Description == description).SingleOrDefault();
return
field ==
null
?
default
(T) :
(T)ConstantValue();
}
}
2.2 Xls
导出类
class ExporterXls
using
System;
using c;
using
using
using
using
using
using
using entModel;
namespace
{
public class ExporterXls : IExporter
{
public override
void Export(ble dt, bool isShow = false)
{
SaveFileDialog
saveFileDialog1 =
IniSaveFileDialog(cri
ptionFromEnumValue());
if (alog() == )
{
throw new Exception((
导出中断,原因
:
用户取消操作!
< br>
}
ExportAsTxt(dt,
saveFileDialog1);
_ExportSaveFileName = me;
if
(isShow)
{
OpenFile(me);
}
}
///
///
导出
excel
,实际为
txt
格式
///
///
///
private void ExportAsTxt(ble dt,
SaveFileDialog saveFileDialog1)
{
Stream myStream;
myStream = le();
//StreamWriter
sw=new
StreamWriter(myStream,oding(
StreamWriter sw = new
StreamWriter(myStream,
oding(
String str =
//
写标题
for
(int i = 0; i < i++)
{
if
(i > 0)
{
str +=
}
str += s[i].ColumnName;
}
ine(str);
//
写内容
for (int rowNo = 0; rowNo < rowNo++)
{
String tempstr =
for (int columnNo = 0; columnNo <
columnNo++)
{
if (columnNo >
0)
{
tempstr +=
}
//tempstr+=[rowNo,columnNo].ToString();
tempstr +=
[rowNo][columnNo].ToString();
}
ine(tempstr);
}
();
();
}
///
///
导出
EXCEL,
适用于数据量不大的情况,
(条目小于
3000
)
///
///
标题
///
///
///
private bool
ExportToExcel(string title, ble dt, bool
isShowExcel =
false)
-
-
-
-
-
-
-
-
-
上一篇:防火阀设置
下一篇:史上最全的CATIA主模块介绍