C#_把dataTable数据导出到CSV,XLS文件
//导出为svc文件
2
public void ExportToSvc(System.Data.DataTable dt,string strName)
3
{
4
string strPath= Path.GetTempPath()+strName+".csv";
5
6
if (File.Exists(strPath))
7
{
8
File.Delete(strPath);
9
}
10
//先打印标头
11
StringBuilder strColu=new StringBuilder();
12
StringBuilder strValue=new StringBuilder();
13
int i=0;
14
15
try
16
{
17
StreamWriter sw = new StreamWriter(new FileStream(strPath, FileMode.CreateNew), Encoding.GetEncoding("GB2312"));
18![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
for( i=0;i<=dt.Columns.Count-1;i++)
20
{
21
strColu.Append(dt.Columns[i].ColumnName);
22
strColu.Append(",");
23
}
24
strColu.Remove(strColu.Length-1,1);//移出掉最后一个,字符
25![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
sw.WriteLine(strColu);
27![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
foreach(DataRow dr in dt.Rows)
29
{
30
strValue.Remove(0,strValue.Length);//移出
31
32
for(i=0;i<=dt.Columns.Count-1;i++)
33
{
34
strValue.Append(dr[i].ToString());
35
strValue.Append(",");
36
}
37
strValue.Remove(strValue.Length-1,1);//移出掉最后一个,字符
38
sw.WriteLine(strValue);
39
}
40
41
sw.Close();
42
}
43
catch(Exception ex)
44
{
45
MessageBox.Show(ex.Message);
46![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
}
48![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
System.Diagnostics.Process.Start(strPath);
50
51
}
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
//*******
作者: wesimy
时间: 07/31/05
版本: v.2.1
函数名: DatatableToCSVFile
参数说明: DataTable dt 导出CSV的数据
string xbkPahth CSV模板,主要存储一些表头的格式
string SavePath 导出的路径
ref string err 出错提示
功能: 把DataTable的数据导出到CSV文件中
********//
public void DatatableToCSVFile(System.Data.DataTable dt, string xbkPath, string SavePath, ref string err)
{
string row;
try
{
string header;
string tmp;
{
string row;
try
{
string header;
string tmp;
StreamReader sr=new StreamReader(xbkPath);
header=sr.ReadLine();
sr.Close();
sr.Close();
FileStream fs=File.Create(SavePath);
StreamWriter sw= new StreamWriter (fs);
StreamWriter sw= new StreamWriter (fs);
sw.WriteLine(header);
foreach(DataRow dr in dt.Rows)
{
row="";
for(int i=0;i<dt.Columns.Count;i++)
{
if(i!=dt.Columns.Count-1)
{
tmp= dr[i].ToString().Trim().Replace(","," ");
row=row+tmp+",";
}
else
{
tmp= dr[i].ToString().Trim().Replace(",",".");
row=row+tmp;
}
}
sw.WriteLine(row);
}
sw.Flush();
{
row="";
for(int i=0;i<dt.Columns.Count;i++)
{
if(i!=dt.Columns.Count-1)
{
tmp= dr[i].ToString().Trim().Replace(","," ");
row=row+tmp+",";
}
else
{
tmp= dr[i].ToString().Trim().Replace(",",".");
row=row+tmp;
}
}
sw.WriteLine(row);
}
sw.Flush();
sw.Close();
}
catch(Exception ex)
{
err=ex.ToString();
}
}
}
catch(Exception ex)
{
err=ex.ToString();
}
}
//*******
作者: wesimy
时间: 07/31/05
版本: v.2.1
函数名: DatatableToExelFile
参数说明: DataTable dt 导出xls的数据
string xbkPahth xls模板,主要存储一些表头的格式
string SavePath 导出的路径
ref string err 出错提示
功能: 把DataTable的数据导出到CSV文件中
说明: 需要用到Excel名称空间等,此外,在不同版本的OFFICE可能会导致在BUILD的时候某些方法的错误,看看参数就可以确定用哪个方法了....
********//
public void DatatableToExcelFile( System.Data.DataTable dt,string xbkPath, string SavePath, ref string err)
{
try
{
Excel.Application excel;
Excel._Workbook xBk ;
Excel._Worksheet xSt;
excel = new Excel.ApplicationClass();
xBk = excel.Workbooks.Open (@xbkPath,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
xSt = (_Worksheet)xBk.Worksheets["OSI Full inspection result"];
{
try
{
Excel.Application excel;
Excel._Workbook xBk ;
Excel._Worksheet xSt;
excel = new Excel.ApplicationClass();
xBk = excel.Workbooks.Open (@xbkPath,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
xSt = (_Worksheet)xBk.Worksheets["OSI Full inspection result"];
int beginRow = 5;
for(int i=0; i<dt.Rows.Count;i++)
{
for(int j=1;j<dt.Columns.Count+1;j++)
{
xSt.Cells[i+beginRow,j]=dt.Rows[i][j-1];
}
}
for(int i=0; i<dt.Rows.Count;i++)
{
for(int j=1;j<dt.Columns.Count+1;j++)
{
xSt.Cells[i+beginRow,j]=dt.Rows[i][j-1];
}
}
xSt.SaveAs(@SavePath,Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
excel.Visible=false;
if(xBk != null)
xBk.Close(false, xbkPath, Type.Missing);
if(xBk != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject (xBk);
xBk=null;
if(xSt != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject (xSt);
xSt = null;
if(excel != null)
{
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject (excel);
excel = null;
}
}
catch(Exception ex)
{
err = ex.Message;
}
}
}
if(xBk != null)
xBk.Close(false, xbkPath, Type.Missing);
if(xBk != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject (xBk);
xBk=null;
if(xSt != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject (xSt);
xSt = null;
if(excel != null)
{
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject (excel);
excel = null;
}
}
catch(Exception ex)
{
err = ex.Message;
}
}
}