原創|其它|編輯:郝浩|2012-10-15 16:42:21.000|閱讀 8671 次
概述:還真沒做過winform的導出導入,今天上網百度了一下。結果---所以還是我自己寫個吧。之前做過web的,半搬半做就OK。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
還真沒做過Aspose.Cells在winform的導出導入,今天上網百度了一下。結果……所以還是我自己寫個吧。之前做過web的,半搬半做就OK。
1、添加引用:Aspose.Cells.dll(我們就叫工具包吧,可以從網上下載。關于它的操作我在“Aspose.Cells操作說明 中文版 下載 Aspose C# 導出Excel 實例”一文中的說。這里你暫時也可不理會它。)
即使沒有安裝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. Word導出
//設置文件類型 // saveFileDialog為一個對話框控件 //如果沒有人工具欄中拉, //可以:SaveFileDialog saveFileDialog1=new SaveFileDialog(); saveFileDialog1.Filter = "導出Excel (*.xls)|*.xls|Word (*.doc)|*.doc"; saveFileDialog1.FilterIndex = 1; 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.Insert(1,"dameng"); saveFileDialog1.FileName = FilePath + "//" + newFileName; System.IO.FileStream fs = (System.IO.FileStream)saveFileDialog1.OpenFile();//輸出文件 StreamWriter writer = new StreamWriter(fs); writer.Write("tttt");//這里就是你要導出到word的內容,內容是你什么你自已DIY writer.Flush(); writer.Close(); fs.Close(); }
4. 導出datatable到excel
DataTable dt = null; if (ds_all.Tables[0] != null) { dt = ds_all.Tables[0]; } else { MessageBox.Show("沒有數據記錄", "*^_^* 溫馨提示信息", MessageBoxButtons.OK); return; } //上面只是取datatable,你自己diy AsposeExcel tt = new AsposeExcel(saveFileDialog1.FileName, "");//不用模板, saveFileDialog1是什么?上面已經說過 bool OK_NO = tt.DatatableToExcel(dt); if (OK_NO) { MessageBox.Show("導出成功", "*^_^* 溫馨提示信息", MessageBoxButtons.OK); } else { }
5. Excel導入
private void 導入ToolStripMenuItem_Click(object sender, EventArgs e) { string localFilePath = ""; //點了保存按鈕進入 if (openFileDialog1.ShowDialog() == DialogResult.OK)// openFileDialog1不要再問我這是什么! { //獲得文件路徑 localFilePath = openFileDialog1.FileName.ToString(); } AsposeExcel tt = new AsposeExcel(localFilePath); DataTable dt; try { dt = tt.ExcelToDatatalbe(); } catch (Exception ex) { return; } //有了datatable你自己就可以DIY啦,下面是我自己的你不用理 if (ddlResidence.SelectedValue == "違章確認") { if (dt.Rows[0][9].ToString() != "違章確認") { return; } row = dt.Rows.Count; if (row <= 0) return; for (int i = 0; i < dt.Rows.Count; i++) { bllviola.Up_Confirmed_ByVnum(dt.Rows[i][6].ToString(), dt.Rows[i][9].ToString()); } this.GridView1.DataSource = dt; GridView1.DataBind(); }
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:何潮的百度空間