C# winform 導出導入Excel/Doc 完整實例教程[網上看到的]


還真沒做過winform的導出導入,今天上網百度了一下。結果---

所以還是我自己寫個吧。之前做過web的,半搬半做就OK。

1添加引用:Aspose.Cells.dll(我們就叫工具包吧,可以從網上下載。關於它的操作我在“Aspose.Cells操作說明 中文版 下載 Aspose C# 導出Excel 實例”一文中的說。這里你暫時也可不理會它。)

Aspose.Cells.dll  和中文說明 下載地址: http://item.taobao.com/auction/item_detail-0db2-f9b1d99e6cb27d78ae9e6373a1529633.htm

即使沒有安裝office也能用噢,這是一個好強的大工具。

2編寫Excel操作類

using System;
using System.Collections.Generic;
using System.Text;
using Aspose.Cells;
using System.Data;

public class AsposeExcel
{

     private string outFileName = "";
     private string fullFilename = "";
     private Workbook book = null;
     private Worksheet sheet = null;


     public AsposeExcel(string outfilename, string tempfilename)//導出構造數
     {
         outFileName = outfilename;
         book = new Workbook();
         // book.Open(tempfilename);這里我們暫時不用模板
         sheet = book.Worksheets[0];
     }
     public AsposeExcel(string fullfilename)//導入構造數
     {
         fullFilename = fullfilename;
         // book = new Workbook();
         //book.Open(tempfilename);
         //sheet = book.Worksheets[0];
     }

     private void AddTitle(string title, int columnCount)
     {
         sheet.Cells.Merge(0, 0, 1, columnCount);
         sheet.Cells.Merge(1, 0, 1, columnCount);

         Cell cell1 = sheet.Cells[0, 0];
         cell1.PutValue(title);
         cell1.Style.HorizontalAlignment = TextAlignmentType.Center;
         cell1.Style.Font.Name = "黑體";
         cell1.Style.Font.Size = 14;
         cell1.Style.Font.IsBold = true;

         Cell cell2 = sheet.Cells[1, 0];
         cell1.PutValue("查詢時間:" + DateTime.Now.ToLocalTime());
         cell2.SetStyle(cell1.Style);
     }

     private void AddHeader(DataTable dt)
     {
         Cell cell = null;
         for (int col = 0; col < dt.Columns.Count; col++)
         {
             cell = sheet.Cells[0, col];
             cell.PutValue(dt.Columns[col].ColumnName);
             cell.Style.Font.IsBold = true;
         }
     }

     private void AddBody(DataTable dt)
     {
         for (int r = 0; r < dt.Rows.Count; r++)
         {
             for (int c = 0; c < dt.Columns.Count; c++)
             {
                 sheet.Cells[r + 1, c].PutValue(dt.Rows[r][c].ToString());
             }
         }
     }
     //導出------------下一篇會用到這個方法
     public Boolean DatatableToExcel(DataTable dt)
     {
         Boolean yn = false;
         try
         {
             //sheet.Name = sheetName;

             //AddTitle(title, dt.Columns.Count);
             //AddHeader(dt);
             AddBody(dt);

             sheet.AutoFitColumns();
             //sheet.AutoFitRows();

             book.Save(outFileName);
             yn = true;
             return yn;
         }
         catch (Exception e)
         {
             return yn;
             // throw e;
         }
     }

     public DataTable ExcelToDatatalbe()//導入
     {
         Workbook book = new Workbook();
         book.Open(fullFilename);
         Worksheet sheet = book.Worksheets[0];
         Cells cells = sheet.Cells;
         //獲取excel中的數據保存到一個datatable中
         DataTable dt_Import = cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, false);
         // dt_Import.
         return dt_Import;
     }
}

3導出按鈕事件中編寫導出數據方法:

private void bt_excel_out_Click(object sender, EventArgs e)
         {
           
             SaveFileDialog saveFileDialog1 = new SaveFileDialog();//如果你直接從對話框中拉出來,就不用new了噢。

           //設置文件類型
             saveFileDialog1.Filter = "導出Excel (*.xls)|*.xls|Word (*.doc)|*.doc";
             saveFileDialog1.FilterIndex = 2;
             saveFileDialog1.RestoreDirectory = true;
             saveFileDialog1.CreatePrompt = true;
             saveFileDialog1.Title = "導出文件保存路徑";
             //saveFileDialog1.ShowDialog();
             //string strName = saveFileDialog1.FileName;

             //設置默認文件類型顯示順序    
             //saveFileDialog1.FilterIndex = 2; 

             //保存對話框是否記憶上次打開的目錄    
             saveFileDialog1.RestoreDirectory = true;

             //點了保存按鈕進入    
             if (saveFileDialog1.ShowDialog() == DialogResult.OK)
             {
                 //獲得文件路徑    
                 string localFilePath = saveFileDialog1.FileName.ToString();

                 //獲取文件名,不帶路徑    
                 string fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1);

                 //獲取文件路徑,不帶文件名    
                 string FilePath = localFilePath.Substring(0, localFilePath.LastIndexOf("\\"));

                 //給文件名前加上時間    
                 string newFileName = DateTime.Now.ToString("yyyyMMdd") + fileNameExt;


                 saveFileDialog1.FileName = FilePath + "\\" + newFileName;
                 System.IO.FileStream fs = (System.IO.FileStream)saveFileDialog1.OpenFile();//輸出文件 

                 //下面是導出數據到doc        導出到excel請看下一篇(發現沒有AsposeExcel
類還沒有用到呢,那正是導出到excel要用的)
                 StreamWriter writer = new StreamWriter(fs);
                 writer.Write("tttt");//這里就是你要導出到word文檔的數據
                 writer.Flush();
                 writer.Close();
                 fs.Close();


             }


         }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM