怎么将DataTable导出到模版Execl并打印
2个回答
2013-08-13
展开全部
刚做过的,以下生成Excel文件的方法是从项目中复制过来的,如果楼主有疑问,请联系我。 /// <summary>
/// 导出Excel表
/// DataTable转化为stream流
/// 直接将NPOI对象写入stream流中
/// </summary>
/// <param name="sourceTable">DataTable数据源</param>
/// <returns></returns>
public static Stream RenderDataTableToExcel(DataTable sourceTable)
{
var workbook = new HSSFWorkbook();
var ms = new MemoryStream();
var sheet = workbook.CreateSheet();
var headerRow = sheet.CreateRow(0); // handling header.
foreach (DataColumn column in sourceTable.Columns)
headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName); // handling value.
var rowIndex = 1; foreach (DataRow row in sourceTable.Rows)
{
var dataRow = sheet.CreateRow(rowIndex);
foreach (DataColumn column in sourceTable.Columns)
{
dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
} rowIndex++;
} workbook.Write(ms);
ms.Flush();
ms.Position = 0;
return ms;
}
}
/// 导出Excel表
/// DataTable转化为stream流
/// 直接将NPOI对象写入stream流中
/// </summary>
/// <param name="sourceTable">DataTable数据源</param>
/// <returns></returns>
public static Stream RenderDataTableToExcel(DataTable sourceTable)
{
var workbook = new HSSFWorkbook();
var ms = new MemoryStream();
var sheet = workbook.CreateSheet();
var headerRow = sheet.CreateRow(0); // handling header.
foreach (DataColumn column in sourceTable.Columns)
headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName); // handling value.
var rowIndex = 1; foreach (DataRow row in sourceTable.Rows)
{
var dataRow = sheet.CreateRow(rowIndex);
foreach (DataColumn column in sourceTable.Columns)
{
dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
} rowIndex++;
} workbook.Write(ms);
ms.Flush();
ms.Position = 0;
return ms;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-08-13
展开全部
Interop.Microsoft.Office.Interop.Excel.dll Interop.Excel.dll用以上两个类库提供的方法 导到Exce文件当中用PrintDocument 对象打印
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询