从数据库导出数据到word、excel、.txt

http://martinyang2010.bokee.com/2598999.html
None.gif 从数据库导出数据到word、excel、.txt -   -
None.gifTag: 导入    word    .doc、.txt、excel    .xls                                           
None.gif
None.gif以下是我最近写的一些关于从数据库导出数据到word、excel、.txt文本文件的代码
None.gif
None.gif我也是初次写这方面的东西,写的不好还请大家批评指正!
None.gif
None.gif
using  System;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Data.SqlClient;
None.gif
using  Excel;
None.gif
using  Word;
None.gif
using  System.IO;
None.gif
None.gif
namespace  Common
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif 
/**//// <summary>
InBlock.gif 
/// 把数据导入到.doc、.txt、.xls文件中
ExpandedSubBlockEnd.gif 
/// </summary>

InBlock.gif public class Export
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  
private const string DATAWORDPATH = @"C:\folder\doc\datadoc\";
InBlock.gif  
private const string IMAGEWORDPATH = @"C:\folder\doc\imagedoc\";
InBlock.gif  
private const string IMAGEPATH = @"C:\folder\image\";
InBlock.gif  
private const string EXCELPATH = @"C:\folder\excel\";
InBlock.gif  
private const string TXTPATH = @"C:\folder\txt\";
InBlock.gif  
private const string IMAGEPOSTFIX = ".bmp";
InBlock.gif  
private const string WORDPOSTFIX = ".doc";
InBlock.gif  
private const string EXCELPOSTFIX = ".xls";
InBlock.gif  
private const string TXTPOSTFIX = ".txt";
InBlock.gif  
private const int DATADISTANCE = 5;
InBlock.gif  
private const int TABDISTANCE = 8;
InBlock.gif  
InBlock.gif  
public Export()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
//
InBlock.gif   
// TODO: 在此处添加构造函数逻辑
InBlock.gif   
//
ExpandedSubBlockEnd.gif
  }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 获得数据集Dataset--------------------------------用于调试
InBlock.gif  
/// </summary>
ExpandedSubBlockEnd.gif  
/// <returns>Dataset</returns>

InBlock.gif  public DataSet GetData()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
try
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
string sConnectionString;
InBlock.gif    sConnectionString 
= "workstation id=GUOFU;packet size=4096;user id=sa;data source=GUOFU;persist security info=True;initial catalog=YC;password=sc";
InBlock.gif    SqlConnection objConn 
= new SqlConnection(sConnectionString);
InBlock.gif    objConn.Open();                                            
InBlock.gif    SqlDataAdapter daPoint 
= new SqlDataAdapter("Select * From Point", objConn);
InBlock.gif    DataSet dsYC 
= new DataSet("YC");
InBlock.gif    daPoint.FillSchema(dsYC,SchemaType.Mapped, 
"Point");
InBlock.gif    daPoint.Fill(dsYC,
"Point");
InBlock.gif    daPoint 
= new SqlDataAdapter("Select * From Employee", objConn);
InBlock.gif    daPoint.FillSchema(dsYC,SchemaType.Mapped, 
"Employee");
InBlock.gif    daPoint.Fill(dsYC,
"Employee");
InBlock.gif    
return dsYC;
ExpandedSubBlockEnd.gif   }

InBlock.gif   
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    
throw new Exception(ex.Message);
ExpandedSubBlockEnd.gif   }

InBlock.gif
ExpandedSubBlockEnd.gif  }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 把数据文件导入到.xls文件
InBlock.gif  
/// </summary>
ExpandedSubBlockEnd.gif  
/// <param name="ds"></param>

InBlock.gif  public void ExportToExcel(DataSet ds)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif
InBlock.gif   
if(ds.Tables.Count!=0)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
//生成.xls文件完整路径名
InBlock.gif
    string tempFileName = GetTempFileName();
InBlock.gif    
object filename = EXCELPATH+tempFileName+EXCELPOSTFIX;
InBlock.gif    
object Nothing = System.Reflection.Missing.Value;
InBlock.gif    
InBlock.gif    
//创建excel文件,文件名用系统时间生成精确到毫秒
InBlock.gif
    Excel.Application myExcel = new Excel.ApplicationClass();
InBlock.gif    myExcel.Application.Workbooks.Add(Nothing);
InBlock.gif
InBlock.gif    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     
//把Dataset中的数据插入excel文件中
InBlock.gif
     int totalCount = 0;
InBlock.gif     
for(int k =0;k<ds.Tables.Count;k++)
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      
int row = ds.Tables[k].Rows.Count;
InBlock.gif      
int column = ds.Tables[k].Columns.Count;
InBlock.gif    
InBlock.gif      
for(int i = 0;i<column;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif       myExcel.Cells[totalCount
+2,1+i] = ds.Tables[k].Columns[i].ColumnName;
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif      
for(int i = 0;i<row;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif       
for(int j =0;j<column;j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif{
InBlock.gif        myExcel.Cells[totalCount
+3+i,1+j] = "'" + ds.Tables[k].Rows[i][j].ToString();
ExpandedSubBlockEnd.gif       }

ExpandedSubBlockEnd.gif      }

InBlock.gif      totalCount 
= totalCount + row +4;
ExpandedSubBlockEnd.gif     }

InBlock.gif
InBlock.gif     
try
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      
//保存excel文件到指定的目录下,文件名用系统时间生成精确到毫秒
InBlock.gif
      myExcel.ActiveWorkbook._SaveAs(filename,Nothing,Nothing,Nothing,Nothing,Nothing,XlSaveAsAccessMode.xlExclusive,Nothing,Nothing,Nothing,Nothing);
ExpandedSubBlockEnd.gif     }

InBlock.gif     
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      System.Windows.Forms.MessageBox.Show(
"系统找不到指定目录下的文件:  "+EXCELPATH+tempFileName+EXCELPOSTFIX);
InBlock.gif      
return;
ExpandedSubBlockEnd.gif     }

InBlock.gif     
//让生成的excel文件可见
InBlock.gif
     myExcel.Visible = true;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
catch(Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     System.Windows.Forms.MessageBox.Show(
"向excel文件中写入数据出错:  " + e.Message);
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif   }

InBlock.gif   
else
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    System.Windows.Forms.MessageBox.Show(
"No Data");
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 把数据导入到.doc文件
InBlock.gif  
/// </summary>
ExpandedSubBlockEnd.gif  
/// <param name="ds"></param>

InBlock.gif  public void ExportToWord(DataSet ds)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
if(ds.Tables.Count!=0)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{   
InBlock.gif    
string tempFileName = null;
InBlock.gif    
object filename = null;
InBlock.gif    
InBlock.gif    
object tableBehavior = Word.WdDefaultTableBehavior.wdWord9TableBehavior;
InBlock.gif    
object autoFitBehavior = Word.WdAutoFitBehavior.wdAutoFitFixed;
InBlock.gif
InBlock.gif    
object unit = Word.WdUnits.wdStory;
InBlock.gif    
object extend = System.Reflection.Missing.Value;
InBlock.gif    
object breakType = (int)Word.WdBreakType.wdSectionBreakNextPage;
InBlock.gif
InBlock.gif    
object count = 1;
InBlock.gif    
object character = Word.WdUnits.wdCharacter;
InBlock.gif
InBlock.gif    
object Nothing =  System.Reflection.Missing.Value;
InBlock.gif    
InBlock.gif    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     tempFileName 
= GetTempFileName();
InBlock.gif
InBlock.gif     
//生成.doc文件完整路径名
InBlock.gif
     filename = DATAWORDPATH+tempFileName+WORDPOSTFIX;
InBlock.gif     
InBlock.gif     
//创建一个word文件,文件名用系统时间生成精确到毫秒
InBlock.gif
     Word.Application myWord= new Word.ApplicationClass();
InBlock.gif     Word._Document myDoc 
= new Word.DocumentClass();
InBlock.gif     myDoc 
= myWord.Documents.Add(ref Nothing,ref Nothing,ref Nothing,ref Nothing); 
InBlock.gif     myDoc.Activate();
InBlock.gif
InBlock.gif     
//向把dataset中的表插入到word的文件中
InBlock.gif
    
InBlock.gif     
for(int totalTable = 0;totalTable<ds.Tables.Count;totalTable++)
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      myWord.Application.Selection.TypeText(ds.Tables[totalTable].TableName
+"表的数据如下");
InBlock.gif      myWord.Application.Selection.TypeParagraph();     
InBlock.gif      myWord.Application.Selection.TypeParagraph();
InBlock.gif      Word.Range para 
= myWord.Application.Selection.Range;
InBlock.gif      myDoc.Tables.Add(para,ds.Tables[totalTable].Rows.Count
+1,ds.Tables[totalTable].Columns.Count,ref tableBehavior,ref autoFitBehavior);
InBlock.gif      
for(int column = 0; column<ds.Tables[totalTable].Columns.Count;column++)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif       myDoc.Tables.Item(totalTable
+1).Cell(1,column+1).Range.InsertBefore(ds.Tables[0].Columns[column].ColumnName.Trim());
ExpandedSubBlockEnd.gif      }
    
InBlock.gif      
for(int row = 0;row<ds.Tables[totalTable].Rows.Count;row++)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif       
for(int column = 0;column<ds.Tables[totalTable].Columns.Count;column++)
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif{
InBlock.gif        myDoc.Tables.Item(totalTable
+1).Cell(row+2,column+1).Range.InsertBefore(ds.Tables[totalTable].Rows[row][column].ToString().Trim());
ExpandedSubBlockEnd.gif       }

ExpandedSubBlockEnd.gif      }

InBlock.gif      myWord.Application.Selection.EndKey(
ref unit,ref extend);
InBlock.gif      myWord.Application.Selection.TypeParagraph();
InBlock.gif      myWord.Application.Selection.TypeParagraph();
InBlock.gif      myWord.Application.Selection.InsertBreak(
ref breakType);    
ExpandedSubBlockEnd.gif     }

InBlock.gif     myWord.Application.Selection.TypeBackspace();
InBlock.gif     myWord.Application.Selection.Delete(
ref character,ref count);
InBlock.gif     myWord.Application.Selection.HomeKey(
ref unit,ref extend);
InBlock.gif    
InBlock.gif     
//保存word文件到指定的目录下
InBlock.gif
     try
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      myDoc.SaveAs(
ref filename,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing);
InBlock.gif      myWord.Visible 
= true;
ExpandedSubBlockEnd.gif     }

InBlock.gif     
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      System.Windows.Forms.MessageBox.Show(
"系统找不到指定目录下的文件:  "+DATAWORDPATH+tempFileName+WORDPOSTFIX);
InBlock.gif      
return;
ExpandedSubBlockEnd.gif     }

InBlock.gif     
//让生成的excel文件可见
InBlock.gif
     myWord.Visible = true;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     System.Windows.Forms.MessageBox.Show(
"向word文件中写入数据出错:  " + ex.Message);
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif   }

InBlock.gif   
else
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    System.Windows.Forms.MessageBox.Show(
"No Data");
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 把图片文件导入到.doc文件
InBlock.gif  
/// </summary>
ExpandedSubBlockEnd.gif  
/// <param name="bp"></param>

InBlock.gif  public void ExportToWord(Bitmap bp)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
string tempFileName = null;
InBlock.gif   
string bmpPath = null;
InBlock.gif   
object filename = null;
InBlock.gif   
object Nothing = null;
InBlock.gif   tempFileName 
= GetTempFileName();
InBlock.gif
InBlock.gif 
InBlock.gif   
//生成.bmp文件完整路径名
InBlock.gif
   bmpPath = IMAGEPATH+tempFileName+IMAGEPOSTFIX;
InBlock.gif
InBlock.gif   
//生成.doc文件完整路径名
InBlock.gif
   filename = IMAGEWORDPATH+tempFileName+WORDPOSTFIX;
InBlock.gif   Nothing 
= System.Reflection.Missing.Value;
InBlock.gif 
InBlock.gif   
//创建一个word文件,文件名用系统时间生成精确到毫秒
InBlock.gif
   Word.Application myWord= new Word.ApplicationClass();
InBlock.gif   Word._Document myDoc 
= new Word.DocumentClass();
InBlock.gif   myDoc 
= myWord.Documents.Add(ref Nothing,ref Nothing,ref Nothing,ref Nothing);  
InBlock.gif
InBlock.gif   
try
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
//把bitmap对象保存到系统所生成文件完整路径中
InBlock.gif
    bp.Save(bmpPath);
ExpandedSubBlockEnd.gif   }

InBlock.gif   
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    System.Windows.Forms.MessageBox.Show(
"系统找不到指定目录下的文件:  "+bmpPath);
InBlock.gif    
return;
ExpandedSubBlockEnd.gif   }

InBlock.gif  
InBlock.gif   
try
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
//往word文件中插入图片
InBlock.gif
    myDoc.InlineShapes.AddPicture(bmpPath,ref Nothing,ref Nothing,ref Nothing);
ExpandedSubBlockEnd.gif   }

InBlock.gif   
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    System.Windows.Forms.MessageBox.Show(
"系统找不到指定目录下的文件:  "+bmpPath);
InBlock.gif    
return;
ExpandedSubBlockEnd.gif   }

InBlock.gif  
InBlock.gif   
try
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
//保存word文件到指定的目录下
InBlock.gif
    myDoc.SaveAs(ref filename,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing);
ExpandedSubBlockEnd.gif   }

InBlock.gif   
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    System.Windows.Forms.MessageBox.Show(
"系统找不到指定目录下的文件:  "+IMAGEWORDPATH+tempFileName+WORDPOSTFIX);
InBlock.gif    
return;
ExpandedSubBlockEnd.gif   }

InBlock.gif
InBlock.gif   
//让生成的word文件可见
InBlock.gif
   myWord.Visible = true;
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 把数据文件导入到.txt文件
InBlock.gif  
/// </summary>
ExpandedSubBlockEnd.gif  
/// <param name="ds"></param>

InBlock.gif  public void ExportToTxt(DataSet ds)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif
InBlock.gif   
if(ds.Tables.Count!=0)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
string tempFileName = null;
InBlock.gif    tempFileName 
= GetTempFileName();
InBlock.gif
InBlock.gif  
InBlock.gif    
//创建一个.txt文件,文件名用系统时间生成精确到毫秒
InBlock.gif
    FileInfo file = new FileInfo(TXTPATH+tempFileName+TXTPOSTFIX);
InBlock.gif    StreamWriter textFile 
= null;
InBlock.gif    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     textFile 
= file.CreateText();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     System.Windows.Forms.MessageBox.Show(
"系统找不到指定目录下的文件:  "+TXTPATH+tempFileName+TXTPOSTFIX);
InBlock.gif     
return;
ExpandedSubBlockEnd.gif    }
 
InBlock.gif
InBlock.gif    
//把Dataset中的数据写入.txt文件中
InBlock.gif
    for(int totaltable = 0;totaltable<ds.Tables.Count;totaltable++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     
//统计dataset中当前表的行数
InBlock.gif
     int row = ds.Tables[totaltable].Rows.Count;
InBlock.gif
InBlock.gif     
//统计dataset中当前表的列数
InBlock.gif
     int column = ds.Tables[totaltable].Columns.Count;
InBlock.gif
InBlock.gif     
//用于统计当前表中每列记录中字符数最长的字符串的长度之和
InBlock.gif
     int totalLength = 0;
InBlock.gif
InBlock.gif     
//用于统计标题的长度(dataset中的表名的length+"表的数据如下"的length)
InBlock.gif
     int titleLength = 0;
InBlock.gif
InBlock.gif     
//统计每列记录中字符数最长的字符串的长度
InBlock.gif
     int[] columnLength = new int[column];
InBlock.gif     
for(int i = 0;i<column;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      columnLength[i] 
= ds.Tables[totaltable].Columns[i].ColumnName.ToString().Length;
ExpandedSubBlockEnd.gif     }

InBlock.gif     
for(int i = 0;i<row;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      
for(int j = 0;j<column;j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif       
if(ds.Tables[totaltable].Rows[i][j].ToString().Length>columnLength[j])
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif{
InBlock.gif        columnLength[j]
=ds.Tables[totaltable].Rows[i][j].ToString().Length;
ExpandedSubBlockEnd.gif       }

ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif     }

InBlock.gif
InBlock.gif
InBlock.gif     
//统计当前表中每列记录中字符数最长的字符串的长度之和
InBlock.gif
     for(int i = 0;i<column;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      totalLength 
= totalLength+columnLength[i]+DATADISTANCE;
ExpandedSubBlockEnd.gif     }

InBlock.gif     totalLength 
= totalLength+2*TABDISTANCE-DATADISTANCE;
InBlock.gif
InBlock.gif     
//统计标题的长度(dataset中的当前表名的length+"表的数据如下"的length)
InBlock.gif
     titleLength = ds.Tables[totaltable].TableName.ToString().Length+"表的数据如下".Length*2;
InBlock.gif
InBlock.gif     
//把标题写入.txt文件中
InBlock.gif
     for(int i = 0;i<(int)((totalLength-titleLength)/2);i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      textFile.Write(
' ');
ExpandedSubBlockEnd.gif     }

InBlock.gif     textFile.Write(ds.Tables[totaltable].TableName
+"表的数据如下");
InBlock.gif     textFile.WriteLine();
InBlock.gif     
for(int i = 0;i<totalLength;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      textFile.Write(
'*');
ExpandedSubBlockEnd.gif     }

InBlock.gif     textFile.WriteLine();
InBlock.gif     textFile.Write(
"\t");
InBlock.gif
InBlock.gif     
//把dataset中当前表的字段名写入.txt文件中
InBlock.gif
     for(int i = 0;i<column;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      textFile.Write(ds.Tables[totaltable].Columns[i].ColumnName.ToString());
InBlock.gif      
for(int k = 0;k<columnLength[i]-ds.Tables[totaltable].Columns[i].ColumnName.ToString().Length+DATADISTANCE;k++)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif       textFile.Write(
' ');
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif     }

InBlock.gif     textFile.WriteLine();
InBlock.gif     
for(int i = 0;i<totalLength;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      textFile.Write(
'-');
ExpandedSubBlockEnd.gif     }

InBlock.gif     textFile.WriteLine();
InBlock.gif     textFile.Write(
"\t");
InBlock.gif
InBlock.gif     
//把dataset中当前表的数据写入.txt文件中
InBlock.gif
     for(int i = 0;i<row;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      
for(int j = 0;j<column;j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif       textFile.Write(ds.Tables[totaltable].Rows[i][j].ToString());
InBlock.gif       
for(int k = 0;k<columnLength[j]-ds.Tables[totaltable].Rows[i][j].ToString().Length+DATADISTANCE;k++)
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif{
InBlock.gif        textFile.Write(
' ');
ExpandedSubBlockEnd.gif       }

ExpandedSubBlockEnd.gif      }

InBlock.gif      textFile.WriteLine();
InBlock.gif      textFile.Write(
"\t");
ExpandedSubBlockEnd.gif     }

InBlock.gif     textFile.WriteLine();
InBlock.gif     
for(int i = 0;i<totalLength;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      textFile.Write(
'-');
ExpandedSubBlockEnd.gif     }

InBlock.gif     textFile.WriteLine();
InBlock.gif     textFile.WriteLine();
InBlock.gif     textFile.WriteLine();
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
//关闭当前的StreamWriter流
InBlock.gif
    textFile.Close();
InBlock.gif    System.Windows.Forms.MessageBox.Show(
"数据文件已保存到"+"   "+file.FullName);                 
ExpandedSubBlockEnd.gif   }

InBlock.gif   
else
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    System.Windows.Forms.MessageBox.Show(
"No Data");
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  
public string GetTempFileName()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
return DateTime.Now.ToString("yyyyMMddhhmmssfff");
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif
None.gif
补充:使用以上方法必须对dcom进行配置,给用户使用office的权限。
具体配置方法如下:
1:在服务器上安装office的Excel软件.
2:在"开始"->"运行"中输入dcomcnfg.exe启动"组件服务"
3:依次双击"组件服务"->"计算机"->"我的电脑"->"DCOM配置"
4:在"DCOM配置"中找到"Microsoft Excel 应用程序",在它上面点击右键,然后点击"属性",弹出"Microsoft Excel 应用程序属性"对话框
5:点击"标识"标签,选择"交互式用户"
6:点击"安全"标签,在"启动和激活权限"上点击"自定义",然后点击对应的"编辑"按钮,在弹出的"安全性"对话框中填加一个"NETWORK SERVICE"用户(注意要选择本计算机名),并给它赋予"本地启动"和"本地激活"权限.
7:依然是"安全"标签,在"访问权限"上点击"自定义",然后点击"编辑",在弹出的"安全性"对话框中也填加一个"NETWORK SERVICE"用户,然后赋予"本地访问"权限.
这样,我们便配置好了相应的Excel的DCOM权限.
注意:我是在WIN2003上配置的,在2000上,是配置ASPNET用户

若不进行配置会出现错误
检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80070005。
原因是用户没有使用Excel的权限。
导出到word同样要配置使用word的权限。

转载于:https://www.cnblogs.com/baiwei1977/archive/2007/02/07/642811.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值