You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DS7/DSWeb/EntityDA/ExcelDA.cs

705 lines
33 KiB
C#

2 years ago
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,string columnwidthlist="")
{
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.OliveGreen.Blue.Index;
cell.FillForegroundColor = HSSFColor.OliveGreen.White.Index;
//- 这个是填充的模式可以是网格、花式等。如果需要填充单色请使用SOLID_FOREGROUND
// cellStyle.FillPattern = FillPattern.NoFill;
cell.FillPattern = FillPattern.SolidForeground;
//- 居中
cell.VerticalAlignment = VerticalAlignment.Center;
//var type = table.Columns[j].DataType;
//if (type== typeof(double)|| type == typeof(Int32))
// cell.Alignment = HorizontalAlignment.RIGHT;
//else
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.SetColumnWidth(k, 20 * 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 = FillPattern.NoFill;
////cell.CellStyle.FillPattern = FillPatternType.SOLID_FOREGROUND;
//- 居中
cell.VerticalAlignment = VerticalAlignment.Center;
//var type = table.Columns[k].DataType;
//if (type == typeof(double) || type == typeof(Int32) || type == typeof(decimal))
// cell.Alignment = HorizontalAlignment.RIGHT;
//else
//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;
}
}
#region 填充内容
#endregion
var dataformat = workbook.CreateDataFormat();
ICellStyle style1 = workbook.CreateCellStyle();
style1.Alignment = HorizontalAlignment.Left;
style1.FillPattern = FillPattern.NoFill;
style1.VerticalAlignment = VerticalAlignment.Center;
style1.BorderBottom = BorderStyle.Thin;
style1.BorderLeft = BorderStyle.Thin;
style1.BorderRight = BorderStyle.Thin;
style1.BorderTop = BorderStyle.Thin;
style1.BottomBorderColor = HSSFColor.Black.Index;
style1.LeftBorderColor = HSSFColor.Black.Index;
style1.RightBorderColor = HSSFColor.Black.Index;
style1.TopBorderColor = HSSFColor.Black.Index;
ICellStyle style2 = workbook.CreateCellStyle();
style2.Alignment = HorizontalAlignment.Right;
style2.DataFormat = dataformat.GetFormat("yyyy-mm-dd");
style2.FillPattern = FillPattern.NoFill;
style2.VerticalAlignment = VerticalAlignment.Center;
style2.BorderBottom = BorderStyle.Thin;
style2.BorderLeft = BorderStyle.Thin;
style2.BorderRight = BorderStyle.Thin;
style2.BorderTop = BorderStyle.Thin;
style2.BottomBorderColor = HSSFColor.Black.Index;
style2.LeftBorderColor = HSSFColor.Black.Index;
style2.RightBorderColor = HSSFColor.Black.Index;
style2.TopBorderColor = HSSFColor.Black.Index;
ICellStyle style3 = workbook.CreateCellStyle();
style3.Alignment = HorizontalAlignment.Right;
style3.DataFormat = dataformat.GetFormat("0.00;[Red]-0.00");
style3.FillPattern = FillPattern.NoFill;
style3.VerticalAlignment = VerticalAlignment.Center;
style3.BorderBottom = BorderStyle.Thin;
style3.BorderLeft = BorderStyle.Thin;
style3.BorderRight = BorderStyle.Thin;
style3.BorderTop = BorderStyle.Thin;
style3.BottomBorderColor = HSSFColor.Black.Index;
style3.LeftBorderColor = HSSFColor.Black.Index;
style3.RightBorderColor = HSSFColor.Black.Index;
style3.TopBorderColor = HSSFColor.Black.Index;
//
int rowIndex = 1;
foreach (DataRow row in table.Rows)
{
IRow dataRow = sheet.CreateRow(rowIndex);
foreach (DataColumn column in table.Columns)
{
//导出内容
var newCell = dataRow.CreateCell(column.Ordinal);
string drValue = row[column].ToString();
switch (column.DataType.ToString())
{
case "System.String"://字符串类型
newCell.SetCellValue(drValue);
newCell.CellStyle= style1;
break;
case "System.DateTime"://日期类型
DateTime dateV;
DateTime.TryParse(drValue, out dateV);
if (dateV <= Convert.ToDateTime("1900-01-01"))
{
newCell.SetCellValue("");
}
else
{
newCell.SetCellValue(dateV);
newCell.CellStyle = style2;
}
//newCell.CellStyle.DataFormat = dataformat.GetFormat("yyyy-mm-dd");//格式化显示
//newCell.CellStyle.Alignment = HorizontalAlignment.Left;
break;
case "System.Boolean"://布尔型
bool boolV = false;
bool.TryParse(drValue, out boolV);
newCell.SetCellValue(boolV);
newCell.CellStyle.Alignment = HorizontalAlignment.Left;
break;
case "System.Int16"://整型
case "System.Int32":
case "System.Int64":
case "System.Byte":
int intV = 0;
int.TryParse(drValue, out intV);
newCell.SetCellValue(intV);
newCell.CellStyle.Alignment = HorizontalAlignment.Left;
break;
case "System.Decimal"://浮点型
case "System.Double":
double doubV = 0;
double.TryParse(drValue, out doubV);
newCell.SetCellValue(doubV);
newCell.CellStyle = style3;
break;
case "System.DBNull"://空值处理
newCell.SetCellValue("");
break;
default:
newCell.SetCellValue("");
newCell.CellStyle.Alignment = HorizontalAlignment.Left;
break;
}
// var type = row[column].GetType();
// if (type == typeof(double) || type == typeof(Int32) || type == typeof(decimal))
// {
// var datacell = dataRow.CreateCell(column.Ordinal, CellType.Numeric);
//// datacell.CellStyle.Alignment = HorizontalAlignment.RIGHT;
// var dataformat = workbook.CreateDataFormat();
// try
// {
// if (row[column] == null)
// {
// datacell.SetCellValue(Convert.ToDouble("0.00"));
// }
// else
// {
// datacell.SetCellValue(Convert.ToDouble(row[column].ToString()));
// }
// datacell.SetCellType(CellType.Numeric);
// if (type == typeof(decimal))
// datacell.CellStyle.DataFormat= dataformat.GetFormat("0.00;[Red]-0.00");
// }
// catch
// {
// datacell.SetCellValue(row[column].ToString());
// }
// }
// else
// {
// var datacell = dataRow.CreateCell(column.Ordinal);
//// datacell.CellStyle.Alignment = HorizontalAlignment.Left;
// datacell.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.OliveGreen.Blue.Index;
cell.FillForegroundColor = HSSFColor.OliveGreen.White.Index;
//- 这个是填充的模式可以是网格、花式等。如果需要填充单色请使用SOLID_FOREGROUND
// cellStyle.FillPattern = FillPattern.NoFill;
cell.FillPattern = FillPattern.SolidForeground;
//- 居中
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 = FillPattern.NoFill;
////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.OliveGreen.Blue.Index;
fontcolorblue.Color = HSSFColor.OliveGreen.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.OliveGreen.Black.Index;
cellStyle.TopBorderColor = HSSFColor.OliveGreen.Black.Index;
//背景图形,我没有用到过。感觉很丑
//cellStyle.FillBackgroundColor = HSSFColor.OliveGreen.Blue.Index;
//cellStyle.FillForegroundColor = HSSFColor.OliveGreen.Blue.Index;
cellStyle.FillForegroundColor = HSSFColor.White.Index;
// cellStyle.FillPattern = FillPattern.NoFill;
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 = FontUnderlineType.Single;
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.OliveGreen.Blue.Index;
cell.FillForegroundColor = HSSFColor.OliveGreen.White.Index;
cell.FillPattern = FillPattern.SolidForeground;
//- 居中
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 = FillPattern.NoFill;
//- 居中
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
}
}