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();
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/25897606/viewspace-712497/
,如需转载,请注明出处,否则将追究法律责任。