原創(chuàng)|其它|編輯:郝浩|2012-10-15 16:42:21.000|閱讀 8671 次
概述:還真沒做過winform的導(dǎo)出導(dǎo)入,今天上網(wǎng)百度了一下。結(jié)果---所以還是我自己寫個(gè)吧。之前做過web的,半搬半做就OK。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
還真沒做過Aspose.Cells在winform的導(dǎo)出導(dǎo)入,今天上網(wǎng)百度了一下。結(jié)果……所以還是我自己寫個(gè)吧。之前做過web的,半搬半做就OK。
1、添加引用:Aspose.Cells.dll(我們就叫工具包吧,可以從網(wǎng)上下載。關(guān)于它的操作我在“Aspose.Cells操作說明 中文版 下載 Aspose C# 導(dǎo)出Excel 實(shí)例”一文中的說。這里你暫時(shí)也可不理會它。)
即使沒有安裝office也能用噢,這是一個(gè)好強(qiáng)的大工具。
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)//導(dǎo)出構(gòu)造數(shù) { outFileName = outfilename; book = new Workbook(); // book.Open(tempfilename);這里我們暫時(shí)不用模板 sheet = book.Worksheets[0]; } public AsposeExcel(string fullfilename)//導(dǎo)入構(gòu)造數(shù) { 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("查詢時(shí)間:" + 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()); } } } //導(dǎo)出------------下一篇會用到這個(gè)方法 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()//導(dǎo)入 { Workbook book = new Workbook(); book.Open(fullFilename); Worksheet sheet = book.Worksheets[0]; Cells cells = sheet.Cells; //獲取excel中的數(shù)據(jù)保存到一個(gè)datatable中 DataTable dt_Import = cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, false); // dt_Import. return dt_Import; } }
3. Word導(dǎo)出
//設(shè)置文件類型 // saveFileDialog為一個(gè)對話框控件 //如果沒有人工具欄中拉, //可以:SaveFileDialog saveFileDialog1=new SaveFileDialog(); saveFileDialog1.Filter = "導(dǎo)出Excel (*.xls)|*.xls|Word (*.doc)|*.doc"; saveFileDialog1.FilterIndex = 1; saveFileDialog1.RestoreDirectory = true; saveFileDialog1.CreatePrompt = true; saveFileDialog1.Title = "導(dǎo)出文件保存路徑"; //saveFileDialog1.ShowDialog(); //string strName = saveFileDialog1.FileName; //設(shè)置默認(rèn)文件類型顯示順序 //saveFileDialog1.FilterIndex = 2; //保存對話框是否記憶上次打開的目錄 saveFileDialog1.RestoreDirectory = true; //點(diǎn)了保存按鈕進(jìn)入 if (saveFileDialog1.ShowDialog() == DialogResult.OK) { //獲得文件路徑 string localFilePath = saveFileDialog1.FileName.ToString(); //獲取文件名,不帶路徑 string fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("//") + 1); //獲取文件路徑,不帶文件名 string FilePath = localFilePath.Substring(0, localFilePath.LastIndexOf("//")); //給文件名前加上時(shí)間 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");//這里就是你要導(dǎo)出到word的內(nèi)容,內(nèi)容是你什么你自已DIY writer.Flush(); writer.Close(); fs.Close(); }
4. 導(dǎo)出datatable到excel
DataTable dt = null; if (ds_all.Tables[0] != null) { dt = ds_all.Tables[0]; } else { MessageBox.Show("沒有數(shù)據(jù)記錄", "*^_^* 溫馨提示信息", MessageBoxButtons.OK); return; } //上面只是取datatable,你自己diy AsposeExcel tt = new AsposeExcel(saveFileDialog1.FileName, "");//不用模板, saveFileDialog1是什么?上面已經(jīng)說過 bool OK_NO = tt.DatatableToExcel(dt); if (OK_NO) { MessageBox.Show("導(dǎo)出成功", "*^_^* 溫馨提示信息", MessageBoxButtons.OK); } else { }
5. Excel導(dǎo)入
private void 導(dǎo)入ToolStripMenuItem_Click(object sender, EventArgs e) { string localFilePath = ""; //點(diǎn)了保存按鈕進(jìn)入 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 == "違章確認(rèn)") { if (dt.Rows[0][9].ToString() != "違章確認(rèn)") { 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(); }
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:何潮的百度空間