|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Web;
|
|
|
using System.IO;
|
|
|
using System.Data;
|
|
|
using System.Text;
|
|
|
|
|
|
using NPOI;
|
|
|
using NPOI.HPSF;
|
|
|
using NPOI.HSSF.UserModel;
|
|
|
using NPOI.HSSF.Util;
|
|
|
using NPOI.SS.UserModel;
|
|
|
using NPOI.POIFS;
|
|
|
using NPOI.Util;
|
|
|
|
|
|
namespace DSWeb.EntityDA
|
|
|
{
|
|
|
public class ExcelDA
|
|
|
{
|
|
|
#region 导出标准excel
|
|
|
/// <summary>
|
|
|
/// 导出标准excel
|
|
|
/// </summary>
|
|
|
/// <param name="table">数据集</param>
|
|
|
/// <param name="strTitle">表头</param>
|
|
|
/// <returns></returns>
|
|
|
public static MemoryStream RenderToExcel(DataTable table, string strTitle)
|
|
|
{
|
|
|
MemoryStream ms = new MemoryStream();
|
|
|
using (table)
|
|
|
{
|
|
|
IWorkbook workbook = new HSSFWorkbook();
|
|
|
|
|
|
ISheet sheet = workbook.CreateSheet();
|
|
|
IRow headerRow = sheet.CreateRow(0);
|
|
|
//ICell icell1top0 = headerRow.CreateCell(0);
|
|
|
//icell1top0.CellStyle = Getcellstyle(workbook, stylexls.头);
|
|
|
//
|
|
|
//定义几种字体,也可以一种字体,写一些公共属性,然后在下面需要时加特殊的
|
|
|
IFont font12 = workbook.CreateFont();
|
|
|
font12.FontHeightInPoints = 12;
|
|
|
font12.Boldweight = (short)FontBoldWeight.BOLD;//- 加粗
|
|
|
//font12.Color = NPOI.HSSF.Util.HSSFColor.WHITE.index;//白色前景色
|
|
|
|
|
|
IFont font10 = workbook.CreateFont();
|
|
|
font10.FontHeightInPoints = 10;
|
|
|
font10.Boldweight = (short)FontBoldWeight.BOLD;//- 加粗
|
|
|
//font10.Color = NPOI.HSSF.Util.HSSFColor.WHITE.index;//白色前景色
|
|
|
//font.Underline = 1;下划线
|
|
|
//
|
|
|
for (int j = 0; j < table.Columns.Count; j++)
|
|
|
{
|
|
|
headerRow.Height = 25 * 20;
|
|
|
//- 创建当前 row 指定列索引的 cell
|
|
|
ICellStyle cell = headerRow.CreateCell(j).CellStyle;//- 标题行
|
|
|
|
|
|
//- 设置这个样式的字体,如果没有设置,将与所有单元格拥有共同字体!
|
|
|
cell.SetFont(font12);
|
|
|
|
|
|
//- 很费解 FillForegroundColor 这个属性,我设置了是背景色,可从字义上来说,这个似乎叫“前景色”?
|
|
|
//- 更令人无语的是,还有 FillBackgroundColor 属性。真不知道做什么的。
|
|
|
//背景图形
|
|
|
cell.FillBackgroundColor = HSSFColor.OLIVE_GREEN.BLUE.index;
|
|
|
cell.FillForegroundColor = HSSFColor.OLIVE_GREEN.WHITE.index;
|
|
|
//- 这个是填充的模式,可以是网格、花式等。如果需要填充单色,请使用:SOLID_FOREGROUND
|
|
|
// cellStyle.FillPattern = FillPatternType.NO_FILL;
|
|
|
cell.FillPattern = FillPatternType.SOLID_FOREGROUND;
|
|
|
|
|
|
//- 居中
|
|
|
cell.VerticalAlignment = VerticalAlignment.CENTER;
|
|
|
cell.Alignment = HorizontalAlignment.CENTER;
|
|
|
|
|
|
//- 细边缘
|
|
|
cell.BorderBottom = BorderStyle.THIN;
|
|
|
cell.BorderLeft = BorderStyle.THIN;
|
|
|
cell.BorderRight = BorderStyle.THIN;
|
|
|
cell.BorderTop = BorderStyle.THIN;
|
|
|
|
|
|
cell.BottomBorderColor = HSSFColor.BLACK.index;
|
|
|
cell.LeftBorderColor = HSSFColor.BLACK.index;
|
|
|
cell.RightBorderColor = HSSFColor.BLACK.index;
|
|
|
cell.TopBorderColor = HSSFColor.BLACK.index;
|
|
|
}
|
|
|
//
|
|
|
if (strTitle.Trim() == "")
|
|
|
{
|
|
|
for (int i = 0; i < table.Columns.Count; i++)
|
|
|
{
|
|
|
headerRow.CreateCell(i).SetCellValue(table.Columns[i].Caption.ToString().Trim());
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
string[] txtval = strTitle.Split(new string[] { "\t" }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
for (int i = 0; i < txtval.Length; i++)
|
|
|
{
|
|
|
headerRow.CreateCell(i).SetCellValue(txtval[i].ToString().Trim());
|
|
|
}
|
|
|
}
|
|
|
////
|
|
|
//foreach (DataColumn column in table.Columns)
|
|
|
//{
|
|
|
// //导入内容
|
|
|
// headerRow.CreateCell(column.Ordinal).SetCellValue(column.Caption);
|
|
|
//}
|
|
|
//
|
|
|
for (int i = 1; i < table.Rows.Count; i++)
|
|
|
{
|
|
|
for (int k = 0; k < table.Columns.Count; k++)
|
|
|
{
|
|
|
IRow headerRow1 = sheet.CreateRow(i);
|
|
|
//ICell icell1top1 = headerRow1.CreateCell(k);
|
|
|
//icell1top1.CellStyle = Getcellstyle(workbook, stylexls.默认);
|
|
|
|
|
|
//- 创建当前 row 指定列索引的 cell
|
|
|
sheet.SetColumnWidth(k, 30 * 256);
|
|
|
//sheet.AutoSizeColumn(k); //会按照值的长短 自动调节列的大小,但是数据量大,会陷入死循环
|
|
|
ICellStyle cell = headerRow1.CreateCell(k).CellStyle;//
|
|
|
//
|
|
|
//- 设置这个样式的字体,如果没有设置,将与所有单元格拥有共同字体!
|
|
|
cell.SetFont(font10);
|
|
|
|
|
|
////- 很费解 FillForegroundColor 这个属性,我设置了是背景色,可从字义上来说,这个似乎叫“前景色”?
|
|
|
////- 更令人无语的是,还有 FillBackgroundColor 属性。真不知道做什么的。
|
|
|
////背景图形
|
|
|
////cell.CellStyle.FillBackgroundColor = HSSFColor.WHITE.index;
|
|
|
////cell.CellStyle.FillForegroundColor = HSSFColor.ORANGE.index;
|
|
|
////- 这个是填充的模式,可以是网格、花式等。如果需要填充单色,请使用:SOLID_FOREGROUND
|
|
|
cell.FillPattern = FillPatternType.NO_FILL;
|
|
|
////cell.CellStyle.FillPattern = FillPatternType.SOLID_FOREGROUND;
|
|
|
|
|
|
//- 居中
|
|
|
cell.VerticalAlignment = VerticalAlignment.CENTER;
|
|
|
cell.Alignment = HorizontalAlignment.LEFT;
|
|
|
|
|
|
//- 细边缘
|
|
|
cell.BorderBottom = BorderStyle.THIN;
|
|
|
cell.BorderLeft = BorderStyle.THIN;
|
|
|
cell.BorderRight = BorderStyle.THIN;
|
|
|
cell.BorderTop = BorderStyle.THIN;
|
|
|
|
|
|
cell.BottomBorderColor = HSSFColor.BLACK.index;
|
|
|
cell.LeftBorderColor = HSSFColor.BLACK.index;
|
|
|
cell.RightBorderColor = HSSFColor.BLACK.index;
|
|
|
cell.TopBorderColor = HSSFColor.BLACK.index;
|
|
|
}
|
|
|
}
|
|
|
//
|
|
|
int rowIndex = 1;
|
|
|
foreach (DataRow row in table.Rows)
|
|
|
{
|
|
|
IRow dataRow = sheet.CreateRow(rowIndex);
|
|
|
foreach (DataColumn column in table.Columns)
|
|
|
{
|
|
|
//导出内容
|
|
|
dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
|
|
|
}
|
|
|
rowIndex++;
|
|
|
}
|
|
|
|
|
|
workbook.Write(ms);
|
|
|
ms.Flush();
|
|
|
ms.Position = 0;
|
|
|
}
|
|
|
return ms;
|
|
|
}
|
|
|
|
|
|
public static MemoryStream RenderToExcel(DataTable table, string strTitle ,int startsheet)
|
|
|
{
|
|
|
MemoryStream ms = new MemoryStream();
|
|
|
using (table)
|
|
|
{
|
|
|
IWorkbook workbook = new HSSFWorkbook();
|
|
|
for ( var _i = 0; _i < startsheet;_i++ )
|
|
|
{
|
|
|
ISheet sheet_0 = workbook.CreateSheet();
|
|
|
}
|
|
|
ISheet sheet = workbook.CreateSheet();
|
|
|
IRow headerRow = sheet.CreateRow(0);
|
|
|
//ICell icell1top0 = headerRow.CreateCell(0);
|
|
|
//icell1top0.CellStyle = Getcellstyle(workbook, stylexls.头);
|
|
|
//
|
|
|
//定义几种字体,也可以一种字体,写一些公共属性,然后在下面需要时加特殊的
|
|
|
IFont font12 = workbook.CreateFont();
|
|
|
font12.FontHeightInPoints = 12;
|
|
|
font12.Boldweight = (short)FontBoldWeight.BOLD;//- 加粗
|
|
|
//font12.Color = NPOI.HSSF.Util.HSSFColor.WHITE.index;//白色前景色
|
|
|
|
|
|
IFont font10 = workbook.CreateFont();
|
|
|
font10.FontHeightInPoints = 10;
|
|
|
font10.Boldweight = (short)FontBoldWeight.BOLD;//- 加粗
|
|
|
//font10.Color = NPOI.HSSF.Util.HSSFColor.WHITE.index;//白色前景色
|
|
|
//font.Underline = 1;下划线
|
|
|
//
|
|
|
for (int j = 0; j < table.Columns.Count; j++)
|
|
|
{
|
|
|
headerRow.Height = 25 * 20;
|
|
|
//- 创建当前 row 指定列索引的 cell
|
|
|
ICellStyle cell = headerRow.CreateCell(j).CellStyle;//- 标题行
|
|
|
|
|
|
//- 设置这个样式的字体,如果没有设置,将与所有单元格拥有共同字体!
|
|
|
cell.SetFont(font12);
|
|
|
|
|
|
//- 很费解 FillForegroundColor 这个属性,我设置了是背景色,可从字义上来说,这个似乎叫“前景色”?
|
|
|
//- 更令人无语的是,还有 FillBackgroundColor 属性。真不知道做什么的。
|
|
|
//背景图形
|
|
|
cell.FillBackgroundColor = HSSFColor.OLIVE_GREEN.BLUE.index;
|
|
|
cell.FillForegroundColor = HSSFColor.OLIVE_GREEN.WHITE.index;
|
|
|
//- 这个是填充的模式,可以是网格、花式等。如果需要填充单色,请使用:SOLID_FOREGROUND
|
|
|
// cellStyle.FillPattern = FillPatternType.NO_FILL;
|
|
|
cell.FillPattern = FillPatternType.SOLID_FOREGROUND;
|
|
|
|
|
|
//- 居中
|
|
|
cell.VerticalAlignment = VerticalAlignment.CENTER;
|
|
|
cell.Alignment = HorizontalAlignment.CENTER;
|
|
|
|
|
|
//- 细边缘
|
|
|
cell.BorderBottom = BorderStyle.THIN;
|
|
|
cell.BorderLeft = BorderStyle.THIN;
|
|
|
cell.BorderRight = BorderStyle.THIN;
|
|
|
cell.BorderTop = BorderStyle.THIN;
|
|
|
|
|
|
cell.BottomBorderColor = HSSFColor.BLACK.index;
|
|
|
cell.LeftBorderColor = HSSFColor.BLACK.index;
|
|
|
cell.RightBorderColor = HSSFColor.BLACK.index;
|
|
|
cell.TopBorderColor = HSSFColor.BLACK.index;
|
|
|
}
|
|
|
//
|
|
|
string[] txtval = strTitle.Split(new string[] { "\t" }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
for (int i = 0; i < txtval.Length; i++)
|
|
|
{
|
|
|
headerRow.CreateCell(i).SetCellValue(txtval[i].ToString().Trim());
|
|
|
}
|
|
|
////
|
|
|
//foreach (DataColumn column in table.Columns)
|
|
|
//{
|
|
|
// //导入内容
|
|
|
// headerRow.CreateCell(column.Ordinal).SetCellValue(column.Caption);
|
|
|
//}
|
|
|
//
|
|
|
for (int i = 1; i < table.Rows.Count; i++)
|
|
|
{
|
|
|
for (int k = 0; k < table.Columns.Count; k++)
|
|
|
{
|
|
|
IRow headerRow1 = sheet.CreateRow(i);
|
|
|
//ICell icell1top1 = headerRow1.CreateCell(k);
|
|
|
//icell1top1.CellStyle = Getcellstyle(workbook, stylexls.默认);
|
|
|
|
|
|
//- 创建当前 row 指定列索引的 cell
|
|
|
sheet.SetColumnWidth(k, 30 * 256);
|
|
|
//sheet.AutoSizeColumn(k); //会按照值的长短 自动调节列的大小,但是数据量大,会陷入死循环
|
|
|
ICellStyle cell = headerRow1.CreateCell(k).CellStyle;//
|
|
|
//
|
|
|
//- 设置这个样式的字体,如果没有设置,将与所有单元格拥有共同字体!
|
|
|
cell.SetFont(font10);
|
|
|
|
|
|
////- 很费解 FillForegroundColor 这个属性,我设置了是背景色,可从字义上来说,这个似乎叫“前景色”?
|
|
|
////- 更令人无语的是,还有 FillBackgroundColor 属性。真不知道做什么的。
|
|
|
////背景图形
|
|
|
////cell.CellStyle.FillBackgroundColor = HSSFColor.WHITE.index;
|
|
|
////cell.CellStyle.FillForegroundColor = HSSFColor.ORANGE.index;
|
|
|
////- 这个是填充的模式,可以是网格、花式等。如果需要填充单色,请使用:SOLID_FOREGROUND
|
|
|
cell.FillPattern = FillPatternType.NO_FILL;
|
|
|
////cell.CellStyle.FillPattern = FillPatternType.SOLID_FOREGROUND;
|
|
|
|
|
|
//- 居中
|
|
|
cell.VerticalAlignment = VerticalAlignment.CENTER;
|
|
|
cell.Alignment = HorizontalAlignment.LEFT;
|
|
|
|
|
|
//- 细边缘
|
|
|
cell.BorderBottom = BorderStyle.THIN;
|
|
|
cell.BorderLeft = BorderStyle.THIN;
|
|
|
cell.BorderRight = BorderStyle.THIN;
|
|
|
cell.BorderTop = BorderStyle.THIN;
|
|
|
|
|
|
cell.BottomBorderColor = HSSFColor.BLACK.index;
|
|
|
cell.LeftBorderColor = HSSFColor.BLACK.index;
|
|
|
cell.RightBorderColor = HSSFColor.BLACK.index;
|
|
|
cell.TopBorderColor = HSSFColor.BLACK.index;
|
|
|
}
|
|
|
}
|
|
|
//
|
|
|
int rowIndex = 1;
|
|
|
foreach (DataRow row in table.Rows)
|
|
|
{
|
|
|
IRow dataRow = sheet.CreateRow(rowIndex);
|
|
|
foreach (DataColumn column in table.Columns)
|
|
|
{
|
|
|
//导出内容
|
|
|
dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
|
|
|
}
|
|
|
rowIndex++;
|
|
|
}
|
|
|
|
|
|
workbook.Write(ms);
|
|
|
ms.Flush();
|
|
|
ms.Position = 0;
|
|
|
}
|
|
|
return ms;
|
|
|
}
|
|
|
|
|
|
public static void RenderToBrowser(MemoryStream ms, HttpContext context, string fileName)
|
|
|
{
|
|
|
if (context.Request.Browser.Browser == "IE")
|
|
|
fileName = HttpUtility.UrlEncode(fileName);
|
|
|
context.Response.AddHeader("Content-Disposition", "attachment;fileName=" + fileName);
|
|
|
context.Response.BinaryWrite(ms.ToArray());
|
|
|
}
|
|
|
|
|
|
#region 定义单元格常用到样式的枚举
|
|
|
public enum stylexls
|
|
|
{
|
|
|
头,
|
|
|
url,
|
|
|
时间,
|
|
|
数字,
|
|
|
钱,
|
|
|
百分比,
|
|
|
中文大写,
|
|
|
科学计数法,
|
|
|
默认
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region 定义单元格常用到样式
|
|
|
static ICellStyle Getcellstyle(IWorkbook wb, stylexls str)
|
|
|
{
|
|
|
ICellStyle cellStyle = wb.CreateCellStyle();
|
|
|
|
|
|
//定义几种字体
|
|
|
//也可以一种字体,写一些公共属性,然后在下面需要时加特殊的
|
|
|
IFont font12 = wb.CreateFont();
|
|
|
font12.FontHeightInPoints = 12;
|
|
|
font12.FontName = "微软雅黑";
|
|
|
font12.Boldweight = (short)FontBoldWeight.BOLD;//- 加粗
|
|
|
|
|
|
IFont font = wb.CreateFont();
|
|
|
font.FontHeightInPoints = 10;
|
|
|
font.FontName = "微软雅黑";
|
|
|
//font.Underline = 1;下划线
|
|
|
|
|
|
IFont fontcolorblue = wb.CreateFont();
|
|
|
fontcolorblue.Color = HSSFColor.OLIVE_GREEN.BLUE.index;
|
|
|
fontcolorblue.IsItalic = true;//下划线
|
|
|
fontcolorblue.FontName = "微软雅黑";
|
|
|
|
|
|
//边框
|
|
|
cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.HAIR;
|
|
|
cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.HAIR;
|
|
|
cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.HAIR;
|
|
|
cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.HAIR;
|
|
|
//边框颜色
|
|
|
cellStyle.BottomBorderColor = HSSFColor.OLIVE_GREEN.BLACK.index;
|
|
|
cellStyle.TopBorderColor = HSSFColor.OLIVE_GREEN.BLACK.index;
|
|
|
|
|
|
//背景图形,我没有用到过。感觉很丑
|
|
|
//cellStyle.FillBackgroundColor = HSSFColor.OLIVE_GREEN.BLUE.index;
|
|
|
//cellStyle.FillForegroundColor = HSSFColor.OLIVE_GREEN.BLUE.index;
|
|
|
cellStyle.FillForegroundColor = HSSFColor.WHITE.index;
|
|
|
// cellStyle.FillPattern = FillPatternType.NO_FILL;
|
|
|
cellStyle.FillBackgroundColor = HSSFColor.BLUE.index;
|
|
|
|
|
|
//水平对齐
|
|
|
cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.LEFT;
|
|
|
|
|
|
//垂直对齐
|
|
|
cellStyle.VerticalAlignment = VerticalAlignment.CENTER;
|
|
|
|
|
|
//自动换行
|
|
|
cellStyle.WrapText = true;
|
|
|
|
|
|
//缩进;当设置为1时,前面留的空白太大了。希旺官网改进。或者是我设置的不对
|
|
|
cellStyle.Indention = 0;
|
|
|
|
|
|
//上面基本都是设共公的设置
|
|
|
//下面列出了常用的字段类型
|
|
|
switch (str)
|
|
|
{
|
|
|
case stylexls.头:
|
|
|
// cellStyle.FillPattern = FillPatternType.LEAST_DOTS;
|
|
|
cellStyle.SetFont(font12);
|
|
|
break;
|
|
|
case stylexls.时间:
|
|
|
IDataFormat datastyle = wb.CreateDataFormat();
|
|
|
cellStyle.DataFormat = datastyle.GetFormat("yyyy/mm/dd");
|
|
|
cellStyle.SetFont(font);
|
|
|
break;
|
|
|
case stylexls.数字:
|
|
|
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00");
|
|
|
cellStyle.SetFont(font);
|
|
|
break;
|
|
|
case stylexls.钱:
|
|
|
IDataFormat format = wb.CreateDataFormat();
|
|
|
cellStyle.DataFormat = format.GetFormat("¥#,##0");
|
|
|
cellStyle.SetFont(font);
|
|
|
break;
|
|
|
case stylexls.url:
|
|
|
fontcolorblue.Underline = 1;
|
|
|
cellStyle.SetFont(fontcolorblue);
|
|
|
break;
|
|
|
case stylexls.百分比:
|
|
|
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");
|
|
|
cellStyle.SetFont(font);
|
|
|
break;
|
|
|
case stylexls.中文大写:
|
|
|
IDataFormat format1 = wb.CreateDataFormat();
|
|
|
cellStyle.DataFormat = format1.GetFormat("[DbNum2][$-804]0");
|
|
|
cellStyle.SetFont(font);
|
|
|
break;
|
|
|
case stylexls.科学计数法:
|
|
|
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00E+00");
|
|
|
cellStyle.SetFont(font);
|
|
|
break;
|
|
|
case stylexls.默认:
|
|
|
cellStyle.SetFont(font);
|
|
|
break;
|
|
|
}
|
|
|
return cellStyle;
|
|
|
}
|
|
|
#endregion
|
|
|
#endregion
|
|
|
|
|
|
#region NPOI导出Excel表功能实现(多个工作簿)
|
|
|
public static MemoryStream RenderToExcel(MemoryStream ms, IWorkbook workbook, DataTable table, string strTitle, string sheetname, int n)
|
|
|
{
|
|
|
//MemoryStream ms = new MemoryStream();
|
|
|
using (table)
|
|
|
{
|
|
|
//IWorkbook workbook = new HSSFWorkbook();
|
|
|
ISheet sheet = workbook.CreateSheet(sheetname);
|
|
|
IRow headerRow = sheet.CreateRow(0);
|
|
|
//定义几种字体,也可以一种字体,写一些公共属性,然后在下面需要时加特殊的
|
|
|
IFont font12 = workbook.CreateFont();
|
|
|
font12.FontHeightInPoints = 12;
|
|
|
font12.Boldweight = (short)FontBoldWeight.BOLD;//- 加粗
|
|
|
|
|
|
IFont font10 = workbook.CreateFont();
|
|
|
font10.FontHeightInPoints = 10;
|
|
|
font10.Boldweight = (short)FontBoldWeight.BOLD;//- 加粗
|
|
|
//
|
|
|
for (int j = 0; j < table.Columns.Count; j++)
|
|
|
{
|
|
|
headerRow.Height = 25 * 20;
|
|
|
//- 创建当前 row 指定列索引的 cell
|
|
|
ICellStyle cell = headerRow.CreateCell(j).CellStyle;//- 标题行
|
|
|
cell.SetFont(font12);
|
|
|
//背景图形
|
|
|
cell.FillBackgroundColor = HSSFColor.OLIVE_GREEN.BLUE.index;
|
|
|
cell.FillForegroundColor = HSSFColor.OLIVE_GREEN.WHITE.index;
|
|
|
cell.FillPattern = FillPatternType.SOLID_FOREGROUND;
|
|
|
|
|
|
//- 居中
|
|
|
cell.VerticalAlignment = VerticalAlignment.CENTER;
|
|
|
cell.Alignment = HorizontalAlignment.CENTER;
|
|
|
|
|
|
//- 细边缘
|
|
|
cell.BorderBottom = BorderStyle.THIN;
|
|
|
cell.BorderLeft = BorderStyle.THIN;
|
|
|
cell.BorderRight = BorderStyle.THIN;
|
|
|
cell.BorderTop = BorderStyle.THIN;
|
|
|
|
|
|
cell.BottomBorderColor = HSSFColor.BLACK.index;
|
|
|
cell.LeftBorderColor = HSSFColor.BLACK.index;
|
|
|
cell.RightBorderColor = HSSFColor.BLACK.index;
|
|
|
cell.TopBorderColor = HSSFColor.BLACK.index;
|
|
|
}
|
|
|
//
|
|
|
string[] txtval = strTitle.Split(new string[] { "\t" }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
for (int i = 0; i < txtval.Length; i++)
|
|
|
{
|
|
|
headerRow.CreateCell(i).SetCellValue(txtval[i].ToString().Trim());
|
|
|
}
|
|
|
for (int i = 1; i < table.Rows.Count; i++)
|
|
|
{
|
|
|
for (int k = 0; k < table.Columns.Count; k++)
|
|
|
{
|
|
|
IRow headerRow1 = sheet.CreateRow(i);
|
|
|
|
|
|
//- 创建当前 row 指定列索引的 cell
|
|
|
sheet.SetColumnWidth(k, 30 * 256);
|
|
|
ICellStyle cell = headerRow1.CreateCell(k).CellStyle;//
|
|
|
cell.SetFont(font10);
|
|
|
cell.FillPattern = FillPatternType.NO_FILL;
|
|
|
|
|
|
//- 居中
|
|
|
cell.VerticalAlignment = VerticalAlignment.CENTER;
|
|
|
cell.Alignment = HorizontalAlignment.LEFT;
|
|
|
|
|
|
//- 细边缘
|
|
|
cell.BorderBottom = BorderStyle.THIN;
|
|
|
cell.BorderLeft = BorderStyle.THIN;
|
|
|
cell.BorderRight = BorderStyle.THIN;
|
|
|
cell.BorderTop = BorderStyle.THIN;
|
|
|
|
|
|
cell.BottomBorderColor = HSSFColor.BLACK.index;
|
|
|
cell.LeftBorderColor = HSSFColor.BLACK.index;
|
|
|
cell.RightBorderColor = HSSFColor.BLACK.index;
|
|
|
cell.TopBorderColor = HSSFColor.BLACK.index;
|
|
|
}
|
|
|
}
|
|
|
//
|
|
|
int rowIndex = 1;
|
|
|
foreach (DataRow row in table.Rows)
|
|
|
{
|
|
|
IRow dataRow = sheet.CreateRow(rowIndex);
|
|
|
foreach (DataColumn column in table.Columns)
|
|
|
{
|
|
|
//导出内容
|
|
|
dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
|
|
|
}
|
|
|
rowIndex++;
|
|
|
}
|
|
|
|
|
|
|
|
|
//foreach (DataRow dr in dtSource.Rows)
|
|
|
//{
|
|
|
// rowCount++;
|
|
|
// //超出10000条数据 创建新的工作簿
|
|
|
// if (rowCount == 10000)
|
|
|
// {
|
|
|
// rowCount = 1;
|
|
|
// sheetCount++;
|
|
|
// newsheet = excelWorkbook.CreateSheet("Sheet" + sheetCount);
|
|
|
// //循环导出列
|
|
|
// foreach (System.Collections.DictionaryEntry de in ListColumnsName)
|
|
|
// {
|
|
|
// HSSFRow newRow = excelSheet.CreateRow(0);
|
|
|
// HSSFCell newCell = newRow.CreateCell(cellIndex);
|
|
|
|
|
|
// newCell.SetCellValue(de.Value.ToString());
|
|
|
// cellIndex++;
|
|
|
// }
|
|
|
// //end
|
|
|
// }
|
|
|
|
|
|
// HSSFRow newRow = newsheet.CreateRow(rowCount);
|
|
|
// InsertCell(dtSource, dr, newRow, newsheet, excelWorkbook);
|
|
|
//}
|
|
|
|
|
|
workbook.Write(ms);
|
|
|
ms.Flush();
|
|
|
ms.Position = n;
|
|
|
}
|
|
|
return ms;
|
|
|
}
|
|
|
#endregion
|
|
|
}
|
|
|
} |