 | using System; using System.Data; using System.Drawing; using System.Data.SqlClient; using Excel; using Word; using System.IO; namespace Common { /// /// 把数据导入到.doc、.txt、.xls文件中 /// public class Export { private const string DATAWORDPATH = @"C:\folder\doc\datadoc\"; private const string IMAGEWORDPATH = @"C:\folder\doc\imagedoc\"; private const string IMAGEPATH = @"C:\folder\image\"; private const string EXCELPATH = @"C:\folder\excel\"; private const string TXTPATH = @"C:\folder\txt\"; private const string IMAGEPOSTFIX = ".bmp"; private const string WORDPOSTFIX = ".doc"; private const string EXCELPOSTFIX = ".xls"; private const string TXTPOSTFIX = ".txt"; private const int DATADISTANCE = 5; private const int TABDISTANCE = 8; public Export() { // // TODO: 在此处添加构造函数逻辑 // } /// /// 获得数据集Dataset--------------------------------用于调试 /// /// Dataset public DataSet GetData() { try { string sConnectionString; sConnectionString = "workstation id=GUOFU;packet size=4096;user id=sa;data source=GUOFU;persist security info=True;initial catalog=YC;password=sc"; SqlConnection objConn = new SqlConnection(sConnectionString); objConn.Open(); SqlDataAdapter daPoint = new SqlDataAdapter("Select * From Point", objConn); DataSet dsYC = new DataSet("YC"); daPoint.FillSchema(dsYC,SchemaType.Mapped, "Point"); daPoint.Fill(dsYC,"Point"); daPoint = new SqlDataAdapter("Select * From Employee", objConn); daPoint.FillSchema(dsYC,SchemaType.Mapped, "Employee"); daPoint.Fill(dsYC,"Employee"); return dsYC; } catch(Exception ex) { throw new Exception(ex.Message); } } /// /// 把数据文件导入到.xls文件 /// /// public void ExportToExcel(DataSet ds) { if(ds.Tables.Count!=0) { //生成.xls文件完整路径名 string tempFileName = GetTempFileName(); object filename = EXCELPATH+tempFileName+EXCELPOSTFIX; object Nothing = System.Reflection.Missing.Value; //创建excel文件,文件名用系统时间生成精确到毫秒 Excel.Application myExcel = new Excel.ApplicationClass(); myExcel.Application.Workbooks.Add(Nothing); try { //把Dataset中的数据插入excel文件中 int totalCount = 0; for(int k =0;k for(int i = 0;i try { //保存excel文件到指定的目录下,文件名用系统时间生成精确到毫秒 myExcel.ActiveWorkbook._SaveAs(filename,Nothing,Nothing,Nothing,Nothing,Nothing,XlSaveAsAccessMode.xlExclusive,Nothing,Nothing,Nothing,Nothing); } catch { System.视窗系统.Forms.MessageBox.Show("系统未找到指定目录下的文件: "+EXCELPATH+tempFileName+EXCELPOSTFIX); return; } //让生成的excel文件可见 myExcel.Visible = true; } catch(Exception e) { System.视窗系统.Forms.MessageBox.Show("向excel文件中写入数据出错: " + e.Message); } } else { System.视窗系统.Forms.MessageBox.Show("No Data"); } } /// /// 把数据导入到.doc文件 /// /// public void ExportToWord(DataSet ds) { if(ds.Tables.Count!=0) { string tempFileName = null; object filename = null; object tableBehavior = Word.WdDefaultTableBehavior.wdWord9TableBehavior; object autoFitBehavior = Word.WdAutoFitBehavior.wdAutoFitFixed; object unit = Word.WdUnits.wdStory; object extend = System.Reflection.Missing.Value; object breakType = (int)Word.WdBreakType.wdSectionBreakNextPage; object count = 1; object character = Word.WdUnits.wdCharacter; object Nothing = System.Reflection.Missing.Value; try { tempFileName = GetTempFileName(); //生成.doc文件完整路径名 filename = DATAWORDPATH+tempFileName+WORDPOSTFIX; //创建一个word文件,文件名用系统时间生成精确到毫秒 Word.Application myWord= new Word.ApplicationClass(); Word._Document myDoc = new Word.DocumentClass(); myDoc = myWord.Documents.Add(ref Nothing,ref Nothing,ref Nothing,ref Nothing); myDoc.Activate(); //向把dataset中的表插入到word的文件中 for(int totalTable = 0;totalTable /// 把图片文件导入到.doc文件 /// /// public void ExportToWord(Bitmap bp) { string tempFileName = null; string bmpPath = null; object filename = null; object Nothing = null; tempFileName = GetTempFileName(); //生成.bmp文件完整路径名 bmpPath = IMAGEPATH+tempFileName+IMAGEPOSTFIX; //生成.doc文件完整路径名 filename = IMAGEWORDPATH+tempFileName+WORDPOSTFIX; Nothing = System.Reflection.Missing.Value; //创建一个word文件,文件名用系统时间生成精确到毫秒 Word.Application myWord= new Word.ApplicationClass(); Word._Document myDoc = new Word.DocumentClass(); myDoc = myWord.Documents.Add(ref Nothing,ref Nothing,ref Nothing,ref Nothing); try { //把bitmap对象保存到系统所生成文件完整路径中 bp.Save(bmpPath); } catch { System.视窗系统.Forms.MessageBox.Show("系统未找到指定目录下的文件: "+bmpPath); return; } try { //往word文件中插入图片 myDoc.InlineShapes.AddPicture(bmpPath,ref Nothing,ref Nothing,ref Nothing); } catch { System.视窗系统.Forms.MessageBox.Show("系统未找到指定目录下的文件: "+bmpPath); return; } try { //保存word文件到指定的目录下 myDoc.SaveAs(ref filename,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing); } catch { System.视窗系统.Forms.MessageBox.Show("系统未找到指定目录下的文件: "+IMAGEWORDPATH+tempFileName+WORDPOSTFIX); return; } //让生成的word文件可见 myWord.Visible = true; } /// /// 把数据文件导入到.txt文件 /// /// public void ExportToTxt(DataSet ds) { if(ds.Tables.Count!=0) { string tempFileName = null; tempFileName = GetTempFileName(); //创建一个.txt文件,文件名用系统时间生成精确到毫秒 FileInfo file = new FileInfo(TXTPATH+tempFileName+TXTPOSTFIX); StreamWriter textFile = null; try { textFile = file.CreateText(); } catch { System.视窗系统.Forms.MessageBox.Show("系统未找到指定目录下的文件: "+TXTPATH+tempFileName+TXTPOSTFIX); return; } //把Dataset中的数据写入.txt文件中 for(int totaltable = 0;totaltable
| |