C#导出Excel,并且设置Excel单元格格式,合并单元格
2016-01-27 09:20
579 查看
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();//垃圾回收
}
}
}
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();//垃圾回收
}
}
}
相关文章推荐
- c#之加加减减的优先级
- c#之类型转换
- Kinect开发(二) 基于C# WPF的Kinect开发环境配置
- 【C#】.NET提供了哪些类型来实现反射
- 跟siki老师学C#第九天(满满的都是练习)
- C#编写控制台简单聊天机器人
- C# 编译的DLL文件在其他项目中调用时,看不见注释
- C#自定义控件 绘制框
- C# List泛型转换,int,string 转字符,转数组
- WPF动画结束后的行为方式
- POPTEST老李谈Debug和Release的区别(c#)2
- C#中remoting和webservice的区别
- POPTEST老李谈Debug和Release的区别(c#) 1
- C# 该行已经属于还有一个表 的解决方法
- C#实现多文件上传,写到文件夹中,获取文件信息以及下载文件和删除文件
- 解析C#中断言与异常的应用方式及异常处理的流程控制
- c# List内存分配
- C#委托
- C# 堆和栈
- C# 查看动态库的方法