




已閱讀5頁(yè),還剩2頁(yè)未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
將數(shù)據(jù)庫(kù)表導(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啟動(dòng)之前時(shí)間private DateTime afterTime;/Excel啟動(dòng)之后時(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對(duì)象beforeTime = DateTime.Now;app = new ApplicationClass();/app.Visible = true;afterTime = DateTime.Now;/打開模板文件,得到WorkBook對(duì)象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對(duì)象sheet = (Excel.Worksheet)book.Sheets.get_Item(1);/ / 將DataTable數(shù)據(jù)導(dǎo)出到Excel(可動(dòng)態(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(可動(dòng)態(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等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 民警打分具體管理辦法
- 供水公司主業(yè)管理辦法
- 法蘭西國(guó)族認(rèn)同研究:從“國(guó)族傳奇”看歷史演變
- 民國(guó)茶葉消費(fèi)量與產(chǎn)量動(dòng)態(tài)關(guān)系研究
- 內(nèi)部濕度差異對(duì)硬化水泥漿體特性的影響研究
- 公共物品維護(hù)管理辦法
- 變頻器效率優(yōu)化-洞察及研究
- 鞭毛狀微生物阪崎腸桿菌的乳粉檢測(cè)技術(shù)研究
- AI大模型在金融領(lǐng)域反欺詐中的應(yīng)用研究與優(yōu)化策略
- 安全生產(chǎn)事故應(yīng)急預(yù)案范本2020年
- 北京市大興區(qū)2025年初中學(xué)業(yè)水平考試地理真題(含答案)
- 第三代社??ㄅ嘤?xùn)
- 辦公室應(yīng)聘題庫(kù)及答案
- 2025年河北中考地理真題含答案
- 鐵礦尾礦清運(yùn)方案(3篇)
- 國(guó)開機(jī)考答案 管理學(xué)基礎(chǔ)2025-06-27
- 國(guó)家開放大學(xué)《思想道德與法治》社會(huì)實(shí)踐報(bào)告范文一
- 【9語安徽中考卷】2025年安徽省中考招生考試真題語文試卷(真題+答案)
- 2025年空氣過濾器行業(yè)分析報(bào)告
- 同等學(xué)力人員申請(qǐng)碩士學(xué)位電子科學(xué)與技術(shù)學(xué)科綜合水平全國(guó)統(tǒng)一考試大綱(第二版)
- (高清版)DG∕TJ 08-507-2018 高強(qiáng)混凝土抗壓強(qiáng)度無損檢測(cè)技術(shù)標(biāo)準(zhǔn)
評(píng)論
0/150
提交評(píng)論