生成excel的时候有时候需要设置单元格的一些属性,可以参考一下:
range.NumberFormatLocal = "@"; //设置单元格格式为文本
ange.get_Range("B1","B"+dataGridView2.RowCount+).NumberFormat = "yyyy-m-d hh:mm:ss"; //时间格式的设置
range = (Range)worksheet.get_Range("A1", "E1"); //获取Excel多个单元格区域:本例做为Excel表头
range.Merge(); //单元格合并动作
worksheet.Cells[, ] = "Excel单元格赋值"; //Excel单元格赋值
range.Font.Size = ; //设置字体大小
range.Font.Underline=true; //设置字体是否有下划线
range.Font.Name="黑体"; 设置字体的种类
range.HorizontalAlignment=XlHAlign.xlHAlignCenter; //设置字体在单元格内的对其方式
range.ColumnWidth=; //设置单元格的宽度
range.Cells.Interior.Color=System.Drawing.Color.FromArgb(,,).ToArgb(); //设置单元格的背景色
range.Borders.LineStyle=; //设置单元格边框的粗细
range.BorderAround(XlLineStyle.xlContinuous,XlBorderWeight.xlThick,XlColorIndex.xlColorIndexAutomatic,System.Drawing.Color.Black.ToArgb()); //给单元格加边框
range.EntireColumn.AutoFit(); //自动调整列宽
Range.HorizontalAlignment= xlCenter; // 文本水平居中方式
Range.VerticalAlignment= xlCenter //文本垂直居中方式
Range.WrapText=true; //文本自动换行
Range.Interior.ColorIndex=; //填充颜色为淡紫色
Range.Font.Color=clBlue; //字体颜色
xlsApp.DisplayAlerts=false; //保存Excel的时候,不弹出是否保存的窗口直接进行保存
workbook.SaveCopyAs(temp);///填入完信息之后另存到路径及文件名字
注:要添加COM组件 Microsoft Excel 11.0 Object Library 引用。
具体代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using Excel;
using System.Reflection;
using System.Data;
using System.Data.OleDb;
namespace RecruitmentReport
{
classdoExcel
{
enumColumnName {A1=,B1,C1,D1,E1,F1,G1,H1,I1,J1,K1,L1,M1,N1,O1,P1,Q1,R1,S1,T1,U1,V1,W1,X1,Y1,Z1}
///
/// 导出到Execl
///
/// 数据集
/// 工作部名称
/// 保存路径
/// 标题名
publicvoid doExport(DataSet dt, string strSheetName, string pathloading, string title)
{ int columnIndex = dt.Tables[].Columns.Count;
string cName =((ColumnName)columnIndex).ToString();
Excel.Application excel = new Excel.Application(); //Execl的操作类
Excel.Workbook bookDest =(Excel.Workbook)excel.Workbooks.Add(Missing.Value);
Excel.Worksheet sheetDest = bookDest.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value) as Excel.Worksheet;//给工作薄添加一个Sheet
sheetDest.Name = strSheetName;
for (int i = bookDest.Worksheets.Count; i >; i--)
{
Worksheet wt = (Worksheet)bookDest.Worksheets[i];
if (wt.Name != strSheetName)
{
wt.Delete();
}
}
int rowIndex = ;
int colIndex = ;
Range rngRow = (Excel.Range)sheetDest.Columns[, Type.Missing];
rngRow.UseStandardWidth = ;
Range rngA = (Range)sheetDest.Columns["A", Type.Missing];//设置单元格格式
rngA.NumberFormatLocal = "@";//字符型格式
Range rngJ = (Range)sheetDest.Columns["J", Type.Missing];
rngJ.NumberFormatLocal = "@";
Range rngQ = (Range)sheetDest.Columns["Q", Type.Missing];
rngQ.NumberFormatLocal = "@";
Range rngE = (Range)sheetDest.Columns["E", Type.Missing];
rngE.NumberFormatLocal = @"yyyy-mm-dd";//日期型格式
sheetDest.get_Range("A1", cName).Merge(sheetDest.get_Range("A1", cName).MergeCells);//合并单元格
excel.Application.Workbooks.Add(true);
try
{
Range rngfirst = (Excel.Range)sheetDest.Cells[, ];
sheetDest.Cells[, ] = title + System.DateTime.Now.Month.ToString().PadLeft(, '') + System.DateTime.Now.Day.ToString().PadLeft(, '') + System.DateTime.Now.Year.ToString();
rngfirst.Font.Size = ;
rngfirst.Font.Name = "Calibri";//设置单元格字体
rngfirst.RowHeight = ;
rngfirst.HorizontalAlignment = XlHAlign.xlHAlignCenter;
rngfirst.Font.Bold = true;
rngfirst.Borders.LineStyle = XlLineStyle.xlContinuous;//设置单元格边框
foreach (DataColumn col in dt.Tables[].Columns)
{ colIndex++;
Range rng = (Excel.Range)sheetDest.Cells[, colIndex]; sheetDest.Cells[, colIndex] = col.ColumnName;//Execl中的第一列把DataTable的列名先导进去
rng.Font.Name = "Calibri";
rng.Font.Size = ;
rng.Font.Bold = true;
rng.Font.Color = ConsoleColor.Blue;
rng.HorizontalAlignment = XlHAlign.xlHAlignCenter;
rng.RowHeight = ;
rng.Borders.LineStyle = XlLineStyle.xlContinuous;
rng.ColumnWidth = 15.5;
// sheetDest.Range[1, colIndex].Font.Bold = false; }
//导入数据行 foreach (DataRow row in dt.Tables[].Rows)
{
rowIndex++;
colIndex = ; foreach (DataColumn col in dt.Tables[].Columns)
{
colIndex++;
sheetDest.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
Range rng01 = (Excel.Range)sheetDest.Cells[rowIndex, colIndex];
rng01.HorizontalAlignment = XlHAlign.xlHAlignCenter;
rng01.Borders.LineStyle = XlLineStyle.xlContinuous;
rng01.RowHeight = ;
rng01.Font.Name = "Calibri";
rng01.Font.Size = ; }
}
}
catch { thrownewException(); }
bookDest.Saved = true;
bookDest.SaveCopyAs(pathloading);//保存
excel.Quit();
excel = null;
GC.Collect();//垃圾回收
}
}
}