C# WinForm 导出导入Excel/Doc 完整实例教程[使用Aspose.Cells.dll]

[csharp]  view plain  copy
  1. 1.添加引用:  
  2.   
  3. Aspose.Cells.dll(我们就叫工具包吧,可以从网上下载。关于它的操作我在“Aspose.Cells操作说明 中文版 下载 Aspose C# 导出Excel 实例”一文中的说。这里你暂时也可不理会它。)  
  4. 即使没有安装office也能用噢,这是一个好强的大工具。  
  5. 2.编写Excel操作类  
  6.   
  7. using System;  
  8. using System.Collections.Generic;  
  9. using System.Text;  
  10. using Aspose.Cells;  
  11. using System.Data;  
  12. public class AsposeExcel  
  13. {  
  14.     private string outFileName = "";  
  15.     private string fullFilename = "";  
  16.     private Workbook book = null;  
  17.     private Worksheet sheet = null;  
  18.     public AsposeExcel(string outfilename, string tempfilename) //导出构造数  
  19.     {  
  20.         outFileName = outfilename;  
  21.         book = new Workbook();  
  22.         // book.Open(tempfilename);这里我们暂时不用模板  
  23.         sheet = book.Worksheets[0];  
  24.     }  
  25.     public AsposeExcel(string fullfilename) //导入构造数  
  26.     {  
  27.         fullFilename = fullfilename;  
  28.         // book = new Workbook();  
  29.         // book.Open(tempfilename);  
  30.         // sheet = book.Worksheets[0];  
  31.     }  
  32.     private void AddTitle(string title, int columnCount)  
  33.     {  
  34.         sheet.Cells.Merge(0, 0, 1, columnCount);  
  35.         sheet.Cells.Merge(1, 0, 1, columnCount);  
  36.         Cell cell1 = sheet.Cells[0, 0];  
  37.         cell1.PutValue(title);  
  38.         cell1.Style.HorizontalAlignment = TextAlignmentType.Center;  
  39.         cell1.Style.Font.Name = "黑体";  
  40.         cell1.Style.Font.Size = 14;  
  41.         cell1.Style.Font.IsBold = true;  
  42.         Cell cell2 = sheet.Cells[1, 0];  
  43.         cell1.PutValue("查询时间:" + DateTime.Now.ToLocalTime());  
  44.         cell2.SetStyle(cell1.Style);  
  45.     }  
  46.     private void AddHeader(DataTable dt)  
  47.     {  
  48.         Cell cell = null;  
  49.         for (int col = 0; col < dt.Columns.Count; col++)  
  50.         {  
  51.             cell = sheet.Cells[0, col];  
  52.             cell.PutValue(dt.Columns[col].ColumnName);  
  53.             cell.Style.Font.IsBold = true;  
  54.         }  
  55.     }  
  56.     private void AddBody(DataTable dt)  
  57.     {  
  58.         for (int r = 0; r < dt.Rows.Count; r++)  
  59.         {  
  60.             for (int c = 0; c < dt.Columns.Count; c++)  
  61.             {  
  62.                 sheet.Cells[r + 1, c].PutValue(dt.Rows[R]­[c].ToString());  
  63.             }  
  64.         }  
  65.     }  
  66.     //导出------------下一篇会用到这个方法  
  67.     public Boolean DatatableToExcel(DataTable dt)  
  68.     {  
  69.         Boolean yn = false;  
  70.         try  
  71.         {  
  72.             //sheet.Name = sheetName;  
  73.             //AddTitle(title, dt.Columns.Count);  
  74.             //AddHeader(dt);  
  75.             AddBody(dt);  
  76.             sheet.AutoFitColumns();  
  77.             //sheet.AutoFitRows();  
  78.             book.Save(outFileName);  
  79.             yn = true;  
  80.             return yn;  
  81.         }  
  82.         catch (Exception e)  
  83.         {  
  84.             return yn;  
  85.             // throw e;  
  86.         }  
  87.     }  
  88.     public DataTable ExcelToDatatalbe()//导入  
  89.     {  
  90.         Workbook book = new Workbook();  
  91.         book.Open(fullFilename);  
  92.         Worksheet sheet = book.Worksheets[0];  
  93.         Cells cells = sheet.Cells;  
  94.         //获取excel中的数据保存到一个datatable中  
  95.         DataTable dt_Import = cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, false);  
  96.         // dt_Import.  
  97.         return dt_Import;  
  98.     }  
  99. }[/R]  
  100.   
  101. 3. Word导出  
  102. //设置文件类型  
  103. // saveFileDialog为一个对话框控件  
  104. //如果没有人工具栏中拉,  
  105. //可以:SaveFileDialog saveFileDialog1=new SaveFileDialog();  
  106. saveFileDialog1.Filter = "导出Excel (*.xls)|*.xls|Word (*.doc)|*.doc";  
  107. saveFileDialog1.FilterIndex = 1;  
  108. saveFileDialog1.RestoreDirectory = true;  
  109. saveFileDialog1.CreatePrompt = true;  
  110. saveFileDialog1.Title = "导出文件保存路径";  
  111. //saveFileDialog1.ShowDialog();  
  112. //string strName = saveFileDialog1.FileName;  
  113. //设置默认文件类型显示顺序  
  114. //saveFileDialog1.FilterIndex = 2;  
  115. //保存对话框是否记忆上次打开的目录  
  116. saveFileDialog1.RestoreDirectory = true;  
  117. //点了保存按钮进入  
  118. if (saveFileDialog1.ShowDialog() == DialogResult.OK)  
  119. {  
  120.     //获得文件路径  
  121.     string localFilePath = saveFileDialog1.FileName.ToString();  
  122.     //获取文件名,不带路径  
  123.     string fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1);  
  124.     //获取文件路径,不带文件名  
  125.     string FilePath = localFilePath.Substring(0, localFilePath.LastIndexOf("\\"));  
  126.     //给文件名前加上时间  
  127.     string newFileName = DateTime.Now.ToString("yyyyMMdd") + fileNameExt;  
  128.     //在文件名里加字符  
  129.     //saveFileDialog1.FileName.Insert(1,"dameng");  
  130.     saveFileDialog1.FileName = FilePath + "\\" + newFileName;  
  131.     System.IO.FileStream fs = (System.IO.FileStream)saveFileDialog1.OpenFile();//输出文件  
  132.     StreamWriter writer = new StreamWriter(fs);  
  133.     writer.Write("tttt");//这里就是你要导出到word的内容,内容是你什么你自已DIY  
  134.     writer.Flush();  
  135.     writer.Close();  
  136.     fs.Close();  
  137. }  
  138.   
  139. 4. 导出datatable到excel  
  140.   
  141. DataTable dt = null;  
  142. if (ds_all.Tables[0] != null)  
  143. {  
  144.     dt = ds_all.Tables[0];  
  145. }  
  146. else {  
  147.     MessageBox.Show("没有数据记录""*^_^* 温馨提示信息", MessageBoxButtons.OK);  
  148.     return;  
  149. }  
  150. //上面只是取datatable,你自己diy  
  151. AsposeExcel tt = new AsposeExcel(saveFileDialog1.FileName, "");//不用模板, saveFileDialog1是什么?上面已经说过  
  152. bool OK_NO = tt.DatatableToExcel(dt);  
  153. if (OK_NO)  
  154. {  
  155.     MessageBox.Show("导出成功""*^_^* 温馨提示信息", MessageBoxButtons.OK);  
  156. }  
  157. else  
  158. {  
  159. }  
  160.   
  161. 5. Excel导入  
  162. private void 导入ToolStripMenuItem_Click(object sender, EventArgs e)  
  163. {  
  164.     string localFilePath = "";  
  165.     //点了保存按钮进入  
  166.     if (openFileDialog1.ShowDialog() == DialogResult.OK)// openFileDialog1不要再问我这是什么!  
  167.     {  
  168.         //获得文件路径  
  169.         localFilePath = openFileDialog1.FileName.ToString();  
  170. }  
  171.     AsposeExcel tt = new AsposeExcel(localFilePath);  
  172.     DataTable dt;  
  173.     try  
  174.     {  
  175.         dt = tt.ExcelToDatatalbe();  
  176.     }  
  177.     catch (Exception ex)  
  178.     {  
  179.         return;  
  180.     }  
  181. //有了datatable你自己就可以DIY啦,下面是我自己的你不用理  
  182. if (ddlResidence.SelectedValue == "违章确认")  
  183.     {  
  184.         if (dt.Rows[0][9].ToString() != "违章确认")  
  185.         {  
  186.                             return;  
  187.         }  
  188.         row = dt.Rows.Count;  
  189.         if (row <= 0) return;  
  190.         for (int i = 0; i < dt.Rows.Count; i++)  
  191.         {  
  192.             bllviola.Up_Confirmed_ByVnum(dt.Rows[i][6].ToString(), dt.Rows[i][9].ToString());  
  193.         }  
  194.         this.GridView1.DataSource = dt;  
  195.         GridView1.DataBind();  
  196. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值