注:要添加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=1,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}

/// <summary>

/// 导出到Execl

/// </summary>

/// <param name="dt">数据集</param>

/// <param name="strSheetName">工作部名称</param>

/// <param name="pathloading">保存路径</param>

/// <param name="title">标题名</param>

publicvoid doExport(DataSet dt, string strSheetName, string pathloading, string title)

{

int columnIndex = dt.Tables[0].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 >1; i--)

{

Worksheet wt = (Worksheet)bookDest.Worksheets[i];

if (wt.Name != strSheetName)

{

wt.Delete();

}

}

int rowIndex = 2;

int colIndex = 0;

Range rngRow = (Excel.Range)sheetDest.Columns[1, Type.Missing];

rngRow.UseStandardWidth = 70;

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[1, 1];

sheetDest.Cells[1, 1] = title + System.DateTime.Now.Month.ToString().PadLeft(2, '0') + System.DateTime.Now.Day.ToString().PadLeft(2, '0') + System.DateTime.Now.Year.ToString();

rngfirst.Font.Size = 14;

rngfirst.Font.Name = "Calibri";//设置单元格字体

rngfirst.RowHeight = 18;

rngfirst.HorizontalAlignment = XlHAlign.xlHAlignCenter;

rngfirst.Font.Bold = true;

rngfirst.Borders.LineStyle = XlLineStyle.xlContinuous;//设置单元格边框

foreach (DataColumn col in dt.Tables[0].Columns)

{

colIndex++;

Range rng = (Excel.Range)sheetDest.Cells[2, colIndex];

sheetDest.Cells[2, colIndex] = col.ColumnName;//Execl中的第一列把DataTable的列名先导进去

rng.Font.Name = "Calibri";

rng.Font.Size = 11;

rng.Font.Bold = true;

rng.Font.Color = ConsoleColor.Blue;

rng.HorizontalAlignment = XlHAlign.xlHAlignCenter;

rng.RowHeight = 15;

rng.Borders.LineStyle = XlLineStyle.xlContinuous;

rng.ColumnWidth = 15.5;

//   sheetDest.Range[1, colIndex].Font.Bold = false;

}

//导入数据行

foreach (DataRow row in dt.Tables[0].Rows)

{

rowIndex++;

colIndex = 0;

foreach (DataColumn col in dt.Tables[0].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 = 15;

rng01.Font.Name = "Calibri";

rng01.Font.Size = 11;

}

}

}

catch  { thrownewException(); }

bookDest.Saved = true;

bookDest.SaveCopyAs(pathloading);//保存

excel.Quit();

excel = null;

GC.Collect();//垃圾回收

}

}

}

http://hi.baidu.com/jimpanf/item/6773171847b46e14e2f98637

C#导出Excel,并且设置Excel单元格格式,合并单元格.的更多相关文章

  1. Python生成文本格式的excel\xlwt生成文本格式的excel\Python设置excel单元格格式为文本\Python excel xlwt 文本格式

    Python生成文本格式的excel\xlwt生成文本格式的excel\Python设置excel单元格格式为文本\Python excel xlwt 文本格式 解决: xlwt 中设置单元格样式主要 ...

  2. poi导出Excel报表多表头双层表头、合并单元格

    效果图: controller层方法: /**     *      * 导出Excel报表     * @param request     * @return     *      */    @ ...

  3. 复杂的POI导出Excel表格(多行表头、合并单元格)

    poi导出excel有两种方式: 第一种:从无到有的创建整个excel,通过HSSFWorkbook,HSSFSheet HSSFCell, 等对象一步一步的创建出工作簿,sheet,和单元格,并添加 ...

  4. 利用PHPExcel导出Excel并设置Excel格式以及数据源

    浏览:23969 发布日期:2013/07/24 分类:技术分享 代码有点长,读起来有点累.先来个截图 导出的Excel太宽了,所以将后面的列宽重新调整了再截的图 功能包括: 1.设置单元格格式,包括 ...

  5. C# Excel行高、列宽、合并单元格、单元格边框线、冻结

    private _Workbook _workBook = null;private Worksheet _workSheet = null;private Excel.Application _ex ...

  6. python xlwt 设置单元格样式-合并单元格

    xlwt模块详解--合并单元格 import xlwtworkbook = xlwt.Workbook()worksheet = workbook.add_sheet('My sheet')# 合并第 ...

  7. c# Excel 行高、列宽、合并单元格、单元格边框线、冻结

    http://www.2cto.com/kf/201007/52724.html 新增选择整列: Range = ((Range)(WALeTieSheet.Cells[1, PwCStartCol_ ...

  8. 在Asp.Net MVC中使用NPOI插件实现对Excel的操作(导入,导出,合并单元格,设置样式,输入公式)

    前言 NPOI 是 POI 项目的.NET版本,它不使用 Office COM 组件,不需要安装 Microsoft Office,目前支持 Office 2003 和 2007 版本. 1.整个Ex ...

  9. NPOI之Excel——合并单元格、设置样式、输入公式

    首先建立一个空白的工作簿用作测试,并在其中建立空白工作表,在表中建立空白行,在行中建立单元格,并填入内容: //建立空白工作簿 IWorkbook workbook = new HSSFWorkboo ...

随机推荐

  1. T4:T4 笔记 + Trait 示例

    背景 官方教程:http://msdn.microsoft.com/en-us/library/vstudio/bb126445.aspx. 如果开发环境或编译器内置了对模板的支持而没有善加利用,就算 ...

  2. 重装Eclipse、离线安装ADT、Android SDK

    由于最新的ADT.Android SDK需要最新版本的Eclipse才能使用,我无奈的只好升级Eclipse.看看自己的Eclipse已经两年没有升级了,也是时候升级了.升级前,有很多的顾虑.因为像这 ...

  3. jenkins插件 查看job修改历史

    文章来自:http://www.ciandcd.com文中的代码来自可以从github下载: https://github.com/ciandcd 插件jobConfigHistory(https:/ ...

  4. eclipse 远程调试

    http://blog.sina.com.cn/s/blog_86a6730b0101iean.html 注:远程服务器端可用以下方式替代: iptables -I from_external 3 - ...

  5. dpkg 被中断,您必须手工运行 sudo dpkg -configure -a 解决

    E: dpkg 被中断,您必须手工运行 sudo dpkg --configure -a 解决此问题. E: dpkg 被中断,您必须手工运行 sudo dpkg --configure -a 解决此 ...

  6. Atiti.ui原理与gui理论

    Atiti.ui原理与gui理论 1. 概论2 2. ui的类型2 2.1. RMGUI vs IMGUI2 2.2. Cli2 2.3. Gui2 2.4. Nui natural user int ...

  7. Revit API 获取某墙上洞口的尺寸和位置

    [Transaction(TransactionMode.Manual)] [Regeneration(RegenerationOption.Manual)] public class cmd2012 ...

  8. 在Unicode版Inno Setup中使用ISSkin

    ISSkin是Code jock 公司出品的Inno Setup 皮肤插件,用于为Inno制作的安装程序提供皮肤功能. 自Delphi发布2009之后,Inno Setup 开始出现支持Unicode ...

  9. RTL8710 Flasher

    https://bitbucket.org/rebane/rtl8710_openocd/ rtl8710_openocd / script / rtl8710.ocd # # OpenOCD scr ...

  10. Oracle的 Pfile生成

    SQL> create pfile from spfile; File created. SQL> [oracle@localhost dbs]$ ls -lrttotal 24-rw-r ...