1.office导出excel本地可以成功,服务器怎么也导出不成功,就换用NPOI导出excel 可以成功 可以成功导出gridview或datatble数据
一个小小的功能让我整了好长时间 这里整理下来 帮助更多寻找的人吧
第一步,引用NPOI.dll
第二步,加类
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.Text;
using System.IO;
using NPOI.HPSF;
using NPOI.HSSF.Util;
using NPOI.XSSF.UserModel;
using NPOI.SS.Util;
/// <summary>
/// ExcleHelper 的摘要说明
/// </summary>
///
namespace excelS
{
public class ExcleHelper
{
public ExcleHelper()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static Stream RenderDataTableToExcel(DataTable SourceTable)
{
HSSFWorkbook workbook = new HSSFWorkbook();
MemoryStream ms = new MemoryStream();
HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet();
HSSFRow headerRow = (HSSFRow)sheet.CreateRow(0);
//调整excel 显示的高宽
headerRow.Height = 85 * 4;
sheet.DefaultColumnWidth = 17;
foreach (DataColumn column in SourceTable.Columns)
headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
int rowIndex = 1;
foreach (DataRow row in SourceTable.Rows)
{
HSSFRow dataRow = (HSSFRow)sheet.CreateRow(rowIndex);
dataRow.Height = 85 * 4;
foreach (DataColumn column in SourceTable.Columns)
{
dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
HSSFCell newCell = (HSSFCell)dataRow.CreateCell(column.Ordinal);
string drValue = row[column].ToString();