[csharp] view plain copy 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; } }[/R] 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(); }