iss版本服务器读取_服务器发布IIS之后,.Net 程序读取不到Excel内容

usingSystem.Collections.Generic;usingSystem.Data;usingSystem.IO;usingSystem.Linq;usingNPOI.HSSF.UserModel;usingNPOI.SS.UserModel;usingNPOI.XSSF.UserModel;public classExcelHelper

{public classx2003

{#region Excel2003

///

///将Excel文件中的数据读出到DataTable中(xls)///

///

///

public static DataTable ExcelToTableForXLS(stringfile)

{

DataTable dt= newDataTable();using (FileStream fs = newFileStream(file, FileMode.Open, FileAccess.Read))

{

HSSFWorkbook hssfworkbook= newHSSFWorkbook(fs);

ISheet sheet= hssfworkbook.GetSheetAt(0);//表头

IRow header =sheet.GetRow(sheet.FirstRowNum);

List columns = new List();for (int i = 0; i < header.LastCellNum; i++)

{object obj = GetValueTypeForXLS(header.GetCell(i) asHSSFCell);if (obj == null || obj.ToString() == string.Empty)

{

dt.Columns.Add(new DataColumn("Columns" +i.ToString()));//continue;

}elsedt.Columns.Add(newDataColumn(obj.ToString()));

columns.Add(i);

}//数据

for (int i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; i++)

{

DataRow dr=dt.NewRow();bool hasValue = false;foreach (int j incolumns)

{

dr[j]= GetValueTypeForXLS(sheet.GetRow(i).GetCell(j) asHSSFCell);if (dr[j] != null && dr[j].ToString() != string.Empty)

{

hasValue= true;

}

}if(hasValue)

{

dt.Rows.Add(dr);

}

}

}returndt;

}///

///将DataTable数据导出到Excel文件中(xls)///

///

///

public static void TableToExcelForXLS(DataTable dt, stringfile)

{

HSSFWorkbook hssfworkbook= newHSSFWorkbook();

ISheet sheet= hssfworkbook.CreateSheet("Test");//表头

IRow row = sheet.CreateRow(0);for (int i = 0; i < dt.Columns.Count; i++)

{

ICell cell=row.CreateCell(i);

cell.SetCellValue(dt.Columns[i].ColumnName);

}//数据

for (int i = 0; i < dt.Rows.Count; i++)

{

IRow row1= sheet.CreateRow(i + 1);for (int j = 0; j < dt.Columns.Count; j++)

{

ICell cell=row1.CreateCell(j);

cell.SetCellValue(dt.Rows[i][j].ToString());

}

}//转为字节数组

MemoryStream stream = newMemoryStream();

hssfworkbook.Write(stream);var buf =stream.ToArray();//保存为Excel文件

using (FileStream fs = newFileStream(file, FileMode.Create, FileAccess.Write))

{

fs.Write(buf,0, buf.Length);

fs.Flush();

}

}///

///获取单元格类型(xls)///

///

///

private static objectGetValueTypeForXLS(HSSFCell cell)

{if (cell == null)return null;switch(cell.CellType)

{case CellType.Blank: //BLANK:

return null;case CellType.Boolean: //BOOLEAN:

returncell.BooleanCellValue;case CellType.Numeric: //NUMERIC:

returncell.NumericCellValue;case CellType.String: //STRING:

returncell.StringCellValue;case CellType.Error: //ERROR:

returncell.ErrorCellValue;case CellType.Formula: //FORMULA:

default:return "=" +cell.CellFormula;

}

}#endregion}public classx2007

{#region Excel2007

///

///将Excel文件中的数据读出到DataTable中(xlsx)///

///

///

public static DataTable ExcelToTableForXLSX(stringfile)

{

DataTable dt= newDataTable();using (FileStream fs = newFileStream(file, FileMode.Open, FileAccess.Read))

{

XSSFWorkbook xssfworkbook= newXSSFWorkbook(fs);

ISheet sheet= xssfworkbook.GetSheetAt(0);//表头

IRow header =sheet.GetRow(sheet.FirstRowNum);

List columns = new List();for (int i = 0; i < header.LastCellNum; i++)

{object obj = GetValueTypeForXLSX(header.GetCell(i) asXSSFCell);if (obj == null || obj.ToString() == string.Empty)

{

dt.Columns.Add(new DataColumn("Columns" +i.ToString()));//continue;

}elsedt.Columns.Add(newDataColumn(obj.ToString()));

columns.Add(i);

}//数据

for (int i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; i++)

{

DataRow dr=dt.NewRow();bool hasValue = false;foreach (int j incolumns)

{

dr[j]= GetValueTypeForXLSX(sheet.GetRow(i).GetCell(j) asXSSFCell);if (dr[j] != null && dr[j].ToString() != string.Empty)

{

hasValue= true;

}

}if(hasValue)

{

dt.Rows.Add(dr);

}

}

}returndt;

}///

///将DataTable数据导出到Excel文件中(xlsx)///

///

///

public static void TableToExcelForXLSX(DataTable dt, stringfile)

{

XSSFWorkbook xssfworkbook= newXSSFWorkbook();

ISheet sheet= xssfworkbook.CreateSheet("Test");//表头

IRow row = sheet.CreateRow(0);for (int i = 0; i < dt.Columns.Count; i++)

{

ICell cell=row.CreateCell(i);

cell.SetCellValue(dt.Columns[i].ColumnName);

}//数据

for (int i = 0; i < dt.Rows.Count; i++)

{

IRow row1= sheet.CreateRow(i + 1);for (int j = 0; j < dt.Columns.Count; j++)

{

ICell cell=row1.CreateCell(j);

cell.SetCellValue(dt.Rows[i][j].ToString());

}

}//转为字节数组

MemoryStream stream = newMemoryStream();

xssfworkbook.Write(stream);var buf =stream.ToArray();//保存为Excel文件

using (FileStream fs = newFileStream(file, FileMode.Create, FileAccess.Write))

{

fs.Write(buf,0, buf.Length);

fs.Flush();

}

}///

///获取单元格类型(xlsx)///

///

///

private static objectGetValueTypeForXLSX(XSSFCell cell)

{if (cell == null)return null;switch(cell.CellType)

{case CellType.Blank: //BLANK:

return null;case CellType.Boolean: //BOOLEAN:

returncell.BooleanCellValue;case CellType.Numeric: //NUMERIC:

returncell.NumericCellValue;case CellType.String: //STRING:

returncell.StringCellValue;case CellType.Error: //ERROR:

returncell.ErrorCellValue;case CellType.Formula: //FORMULA:

default:return "=" +cell.CellFormula;

}

}#endregion}public static DataTable GetDataTable(stringfilepath)

{var dt = new DataTable("xls");if (filepath.Last()=='s')

{

dt=x2003.ExcelToTableForXLS(filepath);

}else{

dt=x2007.ExcelToTableForXLSX(filepath);

}returndt;

}

}

以前用到的代码,只能帮到这了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值