




已閱讀5頁,還剩2頁未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
將數(shù)據(jù)庫表導(dǎo)出到Excel,并生成文件(C#實(shí)現(xiàn))關(guān)鍵詞: C#, ASP.NET, Excel 需添加項(xiàng)目引用:1. .NET-System.Data.OracleClient.dll2. COM-Microsoft Excel 11.0 Object Library代碼如下:using System;using System.IO;using System.Data;using System.Reflection;using System.Diagnostics;using System.Configuration;using System.Collections;using Excel;namespace ProtoType/ / 套用模板輸出Excel,生成xls文件和html文件/ Author: Liu Wen/ Date Created: 2006-8/ public class ExportExcel#region variable memberprotected string templateFile = null;protected string excelFile = null;protected string htmlFile = null;protected object missing = Missing.Value;Excel.ApplicationClass app;Excel.Workbook book;Excel.Worksheet sheet;Excel.Range range;private DateTime beforeTime;/Excel啟動之前時(shí)間private DateTime afterTime;/Excel啟動之后時(shí)間#endregion/ / 構(gòu)造函數(shù),將一個(gè)已有Excel工作簿作為模板,并指定輸出路徑/ / Excel模板文件路徑 / Excel輸出文件路徑 / Html輸出文件路徑 public ExportExcel(string templateFile, string excelFile, string htmlFile)if(templateFile = null)throw new Exception(Excel模板文件路徑不能為空!);if(excelFile = null)throw new Exception(Excel輸出文件路徑不能為空!);if(htmlFile = null)throw new Exception(Html輸出文件路徑不能為空!);if(!File.Exists(templateFile)throw new Exception(指定路徑的Excel模板文件不存在!);this.templateFile = templateFile;this.excelFile = excelFile;this.htmlFile = htmlFile;/創(chuàng)建一個(gè)Application對象beforeTime = DateTime.Now;app = new ApplicationClass();/app.Visible = true;afterTime = DateTime.Now;/打開模板文件,得到WorkBook對象trybook = app.Workbooks.Open(templateFile,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing);catch (Exception e)throw e;/得到WorkSheet對象sheet = (Excel.Worksheet)book.Sheets.get_Item(1);/ / 將DataTable數(shù)據(jù)導(dǎo)出到Excel(可動態(tài)插入行)/ / DataTable / 插入行的索引 / 插入列的索引 public void DataTableToExcel(System.Data.DataTable dt, int rowIndex, int colIndex)/range = sheet.get_Range(A7, missing);/range.Value2 = raogerrr;/string str = range.Text.ToString();int rowCount = dt.Rows.Count;/DataTable行數(shù)int colCount = dt.Columns.Count;/DataTable列數(shù)int iRow;int iCol;/將數(shù)據(jù)導(dǎo)出到相應(yīng)的單元格for (iRow = 0; iRow rowCount; iRow+)/插入新行this.InsertRows(sheet, iRow+rowIndex);/填充當(dāng)前行for (iCol = 0; iCol colCount; iCol+)sheet.CellsiRow+rowIndex, iCol+colIndex = dt.RowsiRowiCol.ToString();this.DeleteRows(sheet, rowCount+rowIndex);/Excel.QueryTables qts = sheet.QueryTables;/Excel.QueryTable qt = qts.Add(,);/qt.RefreshStyle = Excel.XlCellInsertionMode.xlInsertEntireRows;/qt.Refresh();/ / 將DataTable數(shù)據(jù)導(dǎo)出到Excel(可動態(tài)插入行)/ / DataTable / 插入數(shù)據(jù)的起始單元格 public void DataTableToExcel(System.Data.DataTable dt, string cellID)int rowIndex = sheet.get_Range(cellID, missing).Row;int colIndex = sheet.get_Range(cellID, missing).Column;int rowCount = dt.Rows.Count;/DataTable行數(shù)int colCount = dt.Columns.Count;/DataTable列數(shù)int iRow;int iCol;/利用二維數(shù)組批量寫入string, array = new stringrowCount,colCount; for (iRow = 0; iRow rowCount; iRow+)for (iCol = 0; iCol colCount; iCol+)arrayiRow,iCol = dt.RowsiRowiCol.ToString();for (iRow = 0; iRow rowCount; iRow+)this.InsertRows(sheet, iRow+rowIndex);this.DeleteRows(sheet, rowCount+rowIndex);range = sheet.get_Range(cellID, missing);range = range.get_Resize(rowCount, colCount);range.Value2 = array;/ / 將DataTable數(shù)據(jù)導(dǎo)出到Excel(固定)/ / DataTable / 插入數(shù)據(jù)的起始單元格 public void DataTableToExcel2(System.Data.DataTable dt, string cellID)int rowCount = dt.Rows.Count;/DataTable行數(shù)int colCount = dt.Columns.Count;/DataTable列數(shù)int
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 水利水電工程節(jié)水項(xiàng)目的試題及答案
- 設(shè)備租賃經(jīng)營權(quán)轉(zhuǎn)讓協(xié)議
- 環(huán)境科學(xué)中的空氣質(zhì)量檢測試題
- 行政管理決策系統(tǒng)試題及答案
- 人工智能輔助公共安全預(yù)警系統(tǒng)協(xié)議
- 行政管理中不可不知的公共關(guān)系學(xué)試題及答案
- 精解2025年經(jīng)濟(jì)法概論考題解析試題及答案
- 沈陽認(rèn)證考試試題及答案
- 寶貝未來測試題及答案
- 高考地理試題及答案
- 植物新生命的開始《向日葵的一生》觀察課件
- 甲狀腺手術(shù)甲狀旁腺保護(hù)
- 時(shí)空異構(gòu)數(shù)據(jù)融合與建模
- 24節(jié)氣-中國人的時(shí)間美學(xué)智慧樹知到期末考試答案章節(jié)答案2024年東北農(nóng)業(yè)大學(xué)
- 人教部編版七(下)語文《愛蓮說》練習(xí)
- JT-T-808-2019道路運(yùn)輸車輛衛(wèi)星定位系統(tǒng)終端通信協(xié)議及數(shù)據(jù)格式
- 關(guān)于菜鳥驛站轉(zhuǎn)讓合同范本
- DL-T1342-2014電氣接地工程用材料及連接件
- 《包裝結(jié)構(gòu)設(shè)計(jì)》完整課件
- 學(xué)校體育工作條例
- 施工現(xiàn)場視頻監(jiān)控系統(tǒng)施工方案
評論
0/150
提交評論