C# excel 多工作薄操作 部份程式碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Net.Mail;
using System.Net;
using System.Net.Mime;
using System.Data.OracleClient;
using System.Web;
using Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.IO;
//建立excel新增WorkBook 和WorkSheet
ApplicationClass excel = new ApplicationClass();
excel.Application.DisplayAlerts = false;
Workbooks workBooks = excel.Workbooks;
Workbook workBook = workBooks.Add(true);
excel.DisplayAlerts = false;
excel.Visible = false;
Missing missing = Missing.Value;
//新增工作薄
workBook.Worksheets.Add(Type.Missing, workBook.Worksheets[iSheetCount], 1, Type.Missing);
tempSheet = (Worksheet)workBook.Worksheets[iSheetCount + 1];
//設定部份樣式
//表頭
sheet.get_Range(excel.Cells[iBasePos[0] - 1, iBasePos[1] + 1], excel.Cells[iBasePos[0], iBasePos[1] + dtResult.Columns.Count]).Merge(0);
sheet.Cells[iBasePos[0] - 1, iBasePos[1] + 1] = "Fresh Yield Report For處級部級";
sheet.get_Range(excel.Cells[iBasePos[0] - 1, iBasePos[1] + 1], excel.Cells[iBasePos[0] - 1, iBasePos[1] + 1]).Font.Bold = true;
sheet.get_Range(excel.Cells[iBasePos[0] - 1, iBasePos[1] + 1], excel.Cells[iBasePos[0] - 1, iBasePos[1] + 1]).Font.Size = 14;
//設定表格為最適應寬度
sheet.get_Range(excel.Cells[iBasePos[0] + 1, iBasePos[1] + 1], excel.Cells[iBasePos[0] + dtResult.Rows.Count + 1, iBasePos[1] + dtResult.Columns.Count]).Columns.AutoFit();
//設定欄位顏色
sheet.get_Range(excel.Cells[iBasePos[0] + 1, iBasePos[1] + 1], excel.Cells[iBasePos[0] + 1, iBasePos[1] + dtResult.Columns.Count]).Interior.ColorIndex = 16;
sheet.get_Range(excel.Cells[iBasePos[0] + 2, iBasePos[1] + 1], excel.Cells[iBasePos[0] + dtResult.Rows.Count + 1, iBasePos[1] + 1]).Interior.ColorIndex = 16;
//居中
sheet.get_Range(excel.Cells[1, 1], excel.Cells[iBasePos[0] + dtResult.Rows.Count + 4, iBasePos[1] + dtResult.Columns.Count]).HorizontalAlignment = XlVAlign.xlVAlignCenter;
sheet.get_Range(excel.Cells[1, 1], excel.Cells[iBasePos[0] + dtResult.Rows.Count + 4, iBasePos[1] + dtResult.Columns.Count]).VerticalAlignment = XlVAlign.xlVAlignCenter;
//邊框
sheet.get_Range(excel.Cells[iBasePos[0] + 1, iBasePos[1] + 1], excel.Cells[iBasePos[0] + dtResult.Rows.Count + 1, iBasePos[1] + dtResult.Columns.Count]).Borders.LineStyle. = 1;
//儲存
workBook.SaveAs(m_FilePath, missing, missing, missing, missing, missing, XlSaveAsAccessMode.xlShared, missing, missing, missing, missing, missing);
workBooks.Close();
excel.Quit();
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Net.Mail;
using System.Net;
using System.Net.Mime;
using System.Data.OracleClient;
using System.Web;
using Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.IO;
//建立excel新增WorkBook 和WorkSheet
ApplicationClass excel = new ApplicationClass();
excel.Application.DisplayAlerts = false;
Workbooks workBooks = excel.Workbooks;
Workbook workBook = workBooks.Add(true);
excel.DisplayAlerts = false;
excel.Visible = false;
Missing missing = Missing.Value;
//新增工作薄
workBook.Worksheets.Add(Type.Missing, workBook.Worksheets[iSheetCount], 1, Type.Missing);
tempSheet = (Worksheet)workBook.Worksheets[iSheetCount + 1];
//設定部份樣式
//表頭
sheet.get_Range(excel.Cells[iBasePos[0] - 1, iBasePos[1] + 1], excel.Cells[iBasePos[0], iBasePos[1] + dtResult.Columns.Count]).Merge(0);
sheet.Cells[iBasePos[0] - 1, iBasePos[1] + 1] = "Fresh Yield Report For處級部級";
sheet.get_Range(excel.Cells[iBasePos[0] - 1, iBasePos[1] + 1], excel.Cells[iBasePos[0] - 1, iBasePos[1] + 1]).Font.Bold = true;
sheet.get_Range(excel.Cells[iBasePos[0] - 1, iBasePos[1] + 1], excel.Cells[iBasePos[0] - 1, iBasePos[1] + 1]).Font.Size = 14;
//設定表格為最適應寬度
sheet.get_Range(excel.Cells[iBasePos[0] + 1, iBasePos[1] + 1], excel.Cells[iBasePos[0] + dtResult.Rows.Count + 1, iBasePos[1] + dtResult.Columns.Count]).Columns.AutoFit();
//設定欄位顏色
sheet.get_Range(excel.Cells[iBasePos[0] + 1, iBasePos[1] + 1], excel.Cells[iBasePos[0] + 1, iBasePos[1] + dtResult.Columns.Count]).Interior.ColorIndex = 16;
sheet.get_Range(excel.Cells[iBasePos[0] + 2, iBasePos[1] + 1], excel.Cells[iBasePos[0] + dtResult.Rows.Count + 1, iBasePos[1] + 1]).Interior.ColorIndex = 16;
//居中
sheet.get_Range(excel.Cells[1, 1], excel.Cells[iBasePos[0] + dtResult.Rows.Count + 4, iBasePos[1] + dtResult.Columns.Count]).HorizontalAlignment = XlVAlign.xlVAlignCenter;
sheet.get_Range(excel.Cells[1, 1], excel.Cells[iBasePos[0] + dtResult.Rows.Count + 4, iBasePos[1] + dtResult.Columns.Count]).VerticalAlignment = XlVAlign.xlVAlignCenter;
//邊框
sheet.get_Range(excel.Cells[iBasePos[0] + 1, iBasePos[1] + 1], excel.Cells[iBasePos[0] + dtResult.Rows.Count + 1, iBasePos[1] + dtResult.Columns.Count]).Borders.LineStyle. = 1;
//儲存
workBook.SaveAs(m_FilePath, missing, missing, missing, missing, missing, XlSaveAsAccessMode.xlShared, missing, missing, missing, missing, missing);
workBooks.Close();
excel.Quit();
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/25897606/viewspace-712497/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- C#使用開源操作庫MiniExcel操作ExcelC#Excel
- Excel快速合併多張Excel工作表教程 Excel工作表怎麼合併?Excel
- Multiple Books多賬薄
- Python操作excel(將多張excel表融合到一張表)PythonExcel
- C#多執行緒系列(3):原子操作C#執行緒
- C# 填充ExcelC#Excel
- excel將一個工作表根據條件拆分成多個工作簿Excel
- AVEVA MARINE C# 程式執行MarJobLauncher工作C#
- xlsxwriter 操作 ExcelExcel
- java操作excelJavaExcel
- EXCEL操作公式Excel公式
- python操作excelPythonExcel
- C#開發之基於NPOI的操作Excel開發體驗C#Excel
- C#程式呼叫FFmpeg操作音視訊C#
- 【向重複工作說不】c#之模擬滑鼠操作C#
- [C#] 程式碼規範C#
- C# 呼叫Python程式碼C#Python
- poi 匯出Excel java程式碼ExcelJava
- python讀寫Excel表格程式碼PythonExcel
- python 操作 Excel 表格PythonExcel
- excel轉json操作ExcelJSON
- java操作excel表格JavaExcel
- 工作程式碼備用
- ExcelWeb指令碼助手,自定義指令碼,批量操作Excel與網頁ExcelWeb指令碼網頁
- win10開啟Excel表格自動會開啟一個sheet1空白工作薄視窗解決步驟Win10Excel
- C#操作jsonC#JSON
- C# 字串操作C#字串
- C# ling to sql 取多條記錄最大時間的例項程式碼C#SQL
- (新手)使用pandas操作EXCELExcel
- Excel操作-NPOI截圖Excel
- 使用Java操作Excel表格JavaExcel
- C# read excel file via ExcelDataReaderC#ExcelLDA
- C# 將PDF轉為ExcelC#Excel
- C# 將Excel轉為XMLC#ExcelXML
- c#多程式通訊,今天,它來了C#
- [C#]C#中字串的操作C#字串
- [C#]C#時間日期操作C#
- C#/VB.NET 在Excel單元格中應用多種字型格式C#Excel
- 工作自動化,替代手工操作,使用python操作MFC、windows程式PythonWindows