导出Execl

1:新建下载按钮。
protected void xxx(){
DataTable dt=数据源
if(dt.Rows.Count>0)

}

public void DataTableToExcel(DataTable dtSource)
{
using (MemoryStream ms = GenerateMemoryStream(dtSource))
{
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
curContext.Response.Clear();
curContext.Response.Buffer = true;

curContext.Response.AddHeader("Content-Disposition", "attachment;filename=DailyPrice.xls");
curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
curContext.Response.ContentType = "application/ms-excel";
curContext.Response.BinaryWrite(ms.ToArray());
//curContext.Response.End();
}
}


public MemoryStream GenerateMemoryStream(DataTable dtSource)
{
//Color LevelOneColor = Color.FromArgb(101,177,208);
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet();
//sheet.SetColumnWidth(0, "".getBytes().length * 2 * 256);
sheet.SetColumnWidth(0, 5766);//设置单元格宽度
sheet.SetColumnWidth(1, 5766);
sheet.SetColumnWidth(2, 5766);
sheet.SetColumnWidth(3, 5766);
sheet.SetColumnWidth(4, 5766);
sheet.SetColumnWidth(5, 5766);
sheet.SetColumnWidth(6, 5766);
sheet.SetColumnWidth(7, 5766);
sheet.SetColumnWidth(8, 5000);
sheet.SetColumnWidth(9, 5766);
sheet.SetColumnWidth(10, 5766);
sheet.SetColumnWidth(11, 5766);
sheet.SetColumnWidth(12, 5766);
sheet.SetColumnWidth(13, 5766);
sheet.SetColumnWidth(14, 5766);
#region 右击文件 属性信息
{
DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
dsi.Company = "NPOI";
workbook.DocumentSummaryInformation = dsi;

SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
si.Author = "文件作者信息"; //填加xls文件作者信息
si.ApplicationName = "创建程序信息"; //填加xls文件创建程序信息
si.LastAuthor = "最后保存者信息"; //填加xls文件最后保存者信息
si.Comments = "作者信息"; //填加xls文件作者信息
si.Title = "标题信息"; //填加xls文件标题信息
si.Subject = "主题信息";//填加文件主题信息
si.CreateDateTime = System.DateTime.Now;
workbook.SummaryInformation = si;
}
#endregion

HSSFCellStyle dateStyle = (HSSFCellStyle)workbook.CreateCellStyle();
HSSFDataFormat format = (HSSFDataFormat)workbook.CreateDataFormat();
dateStyle.DataFormat = format.GetFormat("yyyy-mm-dd");

//取得列宽
int[] arrColWidth = new int[dtSource.Columns.Count];
foreach (DataColumn item in dtSource.Columns)
{
arrColWidth[item.Ordinal] = Encoding.GetEncoding(936).GetBytes(item.ColumnName.ToString()).Length;
}
for (int i = 0; i < dtSource.Rows.Count; i++)
{
for (int j = 0; j < dtSource.Columns.Count; j++)
{
int intTemp = Encoding.GetEncoding(936).GetBytes(dtSource.Rows[i][j].ToString()).Length;
if (intTemp > arrColWidth[j])
{
arrColWidth[j] = intTemp;
}
}
}
int rowIndex = 0;
foreach (DataRow row in dtSource.Rows)
{
#region 新建表,填充表头,填充列头,样式
if (rowIndex == 65535 || rowIndex == 0)
{
if (rowIndex != 0)
{
sheet = (HSSFSheet)workbook.CreateSheet();
}

HSSFCellStyle headFixdStyle = (HSSFCellStyle)workbook.CreateCellStyle();
headFixdStyle.FillForegroundColor = (new HSSFColor.AQUA().GetIndex());
headFixdStyle.FillBackgroundColor = (new HSSFColor.AQUA().GetIndex());
//填充背景色
//headFixdStyle.FillForegroundColor = GetXLColour(workbook, LevelOneColor);
//headFixdStyle.FillBackgroundColor = GetXLColour(workbook, LevelOneColor);
headFixdStyle.FillPattern = HSSFCellStyle.SOLID_FOREGROUND;
headFixdStyle.Alignment = HSSFCellStyle.ALIGN_CENTER;
HSSFFont font_WorkType = (HSSFFont)workbook.CreateFont();
font_WorkType.FontHeightInPoints = 10;
font_WorkType.Color = new HSSFColor.BLACK().GetIndex();
font_WorkType.Boldweight = 700;
headFixdStyle.WrapText = true;
headFixdStyle.SetFont(font_WorkType);

int firstRow = 0;
#region 列头及样式
{
HSSFRow headerRow = (HSSFRow)sheet.CreateRow(firstRow);
HSSFCellStyle headStyle = (HSSFCellStyle)workbook.CreateCellStyle();
headStyle.FillForegroundColor = (new HSSFColor.AQUA().GetIndex());
headStyle.FillBackgroundColor = (new HSSFColor.AQUA().GetIndex());
//填充背景色
//headStyle.FillForegroundColor = GetXLColour(workbook, LevelOneColor);
//headStyle.FillBackgroundColor = GetXLColour(workbook, LevelOneColor);
headStyle.FillPattern = HSSFCellStyle.SOLID_FOREGROUND;
headStyle.Alignment = HSSFCellStyle.ALIGN_CENTER;
HSSFFont font = (HSSFFont)workbook.CreateFont();
font.FontHeightInPoints = 10;
font.Color = new HSSFColor.BLACK().GetIndex();
font.Boldweight = 700;
headStyle.WrapText = true;
headStyle.SetFont(font);

HSSFRow headerSecondRow = (HSSFRow)sheet.CreateRow(firstRow + 1);
for (int c = 0; c < dtSource.Columns.Count; c++)
{
DataColumn column = dtSource.Columns[c];
string columName = column.ColumnName;
int cellIndex = c;
if (cellIndex == 10)
{
headerRow.CreateCell(cellIndex).SetCellValue("Actual Price");
headerRow.GetCell(cellIndex).CellStyle = headFixdStyle;
CellRangeAddress ActualPriceRangeAddress = new CellRangeAddress(firstRow, firstRow, cellIndex, cellIndex + 1);
sheet.AddMergedRegion(ActualPriceRangeAddress);
headerSecondRow.CreateCell(cellIndex).SetCellValue(columName);
headerSecondRow.GetCell(cellIndex).CellStyle = headStyle;
}
else if (cellIndex == 11)
{
headerSecondRow.CreateCell(cellIndex).SetCellValue(columName);
headerSecondRow.GetCell(cellIndex).CellStyle = headStyle;
}
else
{
headerRow.CreateCell(cellIndex).SetCellValue(columName);
headerRow.GetCell(cellIndex).CellStyle = headFixdStyle;
}
}
// headerRow.Dispose();
}
#endregion
CellRangeAddress ProjectNameRangeAddress = new CellRangeAddress(firstRow, firstRow + 1, 0, 0);
sheet.AddMergedRegion(ProjectNameRangeAddress);

CellRangeAddress ProjectCodeRangeAddress = new CellRangeAddress(firstRow, firstRow + 1, 1, 1);
sheet.AddMergedRegion(ProjectCodeRangeAddress);

CellRangeAddress CustomerGroupIDRangeAddress = new CellRangeAddress(firstRow, firstRow + 1, 2, 2);
sheet.AddMergedRegion(CustomerGroupIDRangeAddress);

CellRangeAddress CustomerGroupNameRangeAddress = new CellRangeAddress(firstRow, firstRow + 1, 3, 3);
sheet.AddMergedRegion(CustomerGroupNameRangeAddress);

CellRangeAddress CustomerPnameRangeAddress = new CellRangeAddress(firstRow, firstRow + 1, 4, 4);
sheet.AddMergedRegion(CustomerPnameRangeAddress);

CellRangeAddress ValuationNoRangeAddress = new CellRangeAddress(firstRow, firstRow + 1, 5, 5);
sheet.AddMergedRegion(ValuationNoRangeAddress);

CellRangeAddress RRDPnameRangeAddress = new CellRangeAddress(firstRow, firstRow + 1, 6, 6);
sheet.AddMergedRegion(RRDPnameRangeAddress);

CellRangeAddress ProductNameRangeAddress = new CellRangeAddress(firstRow, firstRow + 1, 7, 7);
sheet.AddMergedRegion(ProductNameRangeAddress);


CellRangeAddress ProductTypeIDRangeAddress = new CellRangeAddress(firstRow, firstRow + 1, 8, 8);
sheet.AddMergedRegion(ProductTypeIDRangeAddress);

CellRangeAddress ProjectOwnerRangeAddress = new CellRangeAddress(firstRow, firstRow + 1, 9, 9);
sheet.AddMergedRegion(ProjectOwnerRangeAddress);

CellRangeAddress WinorLostRangeAddress = new CellRangeAddress(firstRow, firstRow + 1, 12, 12);
sheet.AddMergedRegion(WinorLostRangeAddress);

CellRangeAddress ModifyDateRangeAddress = new CellRangeAddress(firstRow, firstRow + 1, 13, 13);
sheet.AddMergedRegion(ModifyDateRangeAddress);

CellRangeAddress DownloadTimeRangeAddress = new CellRangeAddress(firstRow, firstRow + 1, 14, 14);
sheet.AddMergedRegion(DownloadTimeRangeAddress);

rowIndex = 2;
}
#endregion

//Set row left side Column's Color By Jeson
HSSFCellStyle rowLeftSideStyle = (HSSFCellStyle)workbook.CreateCellStyle();
rowLeftSideStyle.FillForegroundColor = (new HSSFColor.WHITE().GetIndex());
rowLeftSideStyle.FillBackgroundColor = (new HSSFColor.WHITE().GetIndex());
rowLeftSideStyle.FillPattern = HSSFCellStyle.SOLID_FOREGROUND;
rowLeftSideStyle.Alignment = HSSFCellStyle.ALIGN_CENTER;
rowLeftSideStyle.BorderBottom = HSSFCellStyle.BORDER_THIN; // 底部边框
rowLeftSideStyle.BorderLeft = HSSFCellStyle.BORDER_THIN; // 左边边框
rowLeftSideStyle.BorderRight = HSSFCellStyle.BORDER_THIN; // 右边边框
rowLeftSideStyle.BorderTop = HSSFCellStyle.BORDER_THIN; // 左边边框
rowLeftSideStyle.BottomBorderColor = (new HSSFColor.BLACK().GetIndex()); // 底部边框颜色
// rowLeftSideStyle.FillPattern = NPOI.SS.UserModel.FillPatternType.LEAST_DOTS;
// rowLeftSideStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER;
HSSFFont font_leftSide = (HSSFFont)workbook.CreateFont();
font_leftSide.FontHeightInPoints = 10;
font_leftSide.Color = new HSSFColor.BLACK().GetIndex();
//leftSideStyle.WrapText = true;
rowLeftSideStyle.SetFont(font_leftSide);

#region 填充内容
HSSFRow dataRow = (HSSFRow)sheet.CreateRow(rowIndex);
for (int n = 0; n < dtSource.Columns.Count; n++)
{
DataColumn column = dtSource.Columns[n];
string drValue = row[column].ToString();

HSSFCell newCell = (HSSFCell)dataRow.CreateCell(column.Ordinal);
setColumnCellData(newCell, drValue, column);
newCell.CellStyle = rowLeftSideStyle;

}
#endregion
rowIndex++;
}
using (MemoryStream ms = new MemoryStream())
{
workbook.Write(ms);
ms.Flush();
ms.Position = 0;
//sheet.Dispose();
//workbook.Dispose();//一般只用写这一个就OK了,他会遍历并释放所有资源,但当前版本有问题所以只释放sheet
return ms;
}
}

private void setColumnCellData(HSSFCell newCell, string drValue, DataColumn column)
{
switch (column.DataType.ToString())
{
case "System.String"://字符串类型
newCell.SetCellValue(drValue);
break;
case "System.DateTime"://日期类型
// System.DateTime dateV;
//System.DateTime.TryParse(drValue, out dateV);
newCell.SetCellValue(drValue);

// newCell.CellStyle = dateStyle;//格式化显示
break;
case "System.Boolean"://布尔型
bool boolV = false;
bool.TryParse(drValue, out boolV);
newCell.SetCellValue(boolV);
break;
case "System.Int16"://整型
case "System.Int32":
case "System.Int64":
case "System.Byte":
int intV = 0;
int.TryParse(drValue, out intV);
newCell.SetCellValue(intV);
break;
case "System.Decimal"://浮点型
case "System.Double":
double doubV = 0;
double.TryParse(drValue, out doubV);
newCell.SetCellValue(doubV);
break;
case "System.DBNull"://空值处理
newCell.SetCellValue("");
break;
default:
newCell.SetCellValue("");
break;
}
}

转载于:https://www.cnblogs.com/illegal/p/6374324.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值