標籤:winform style http io os ar strong for 檔案
還真沒做過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();
}
}
C# winform 匯出匯入Excel/Doc 完整執行個體教程[網上看到的]