62,073
社区成员
发帖
与我相关
我的任务
分享
/// <param name="startrow">起始行号</param>
/// <param name="endrow">终止行号</param>
/// <param name="filestream">文件流</param>
/// <returns>返回ArrayList</returns>
private ArrayList InputExcel(int startrow, int endrow, Stream filestream)
{
CompoundDocument doc = null;
ArrayList returnValue = new ArrayList();
//读取本地文件
string file = ExcelSavePath;
int sheet = int.Parse(QD.Trim()); //sheet从0开始
int noCol = int.Parse(QDL.Trim()); //清单列
int conUnitCol = int.Parse(DWL.Trim());//单位所在列
int conProjectCol = int.Parse(XMMCL.Trim());//项目名称所在列
try
{
doc = CompoundDocument.Read(filestream); //关键就是这里出错 (截图就是filestream对象的内容)
}
catch (Exception ex)
{
returnValue.Add(ex.Message);
}
if (doc == null) return returnValue;
byte[] bookdata = null;
Workbook book = null;
Worksheet mySheet = null;
try
{
bookdata = doc.GetStreamData("Workbook");
book = new Workbook();
book.Read(new MemoryStream(bookdata));
mySheet = book.Worksheets[sheet];
}
catch (Exception ex)
{
returnValue.Add(ex.Message);
}
}
public static string GetExcelConnectionString(string file)
{
string connectionString = string.Empty;
string fileExtension = file.Substring(file.LastIndexOf(".") + 1);
switch (fileExtension)
{
case "xls":
connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Server.MapPath(file) + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";
break;
case "xlsx":
connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + HttpContext.Current.Server.MapPath(file) + ";Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=1'";
break;
}
return connectionString;
}
DataTable Excel_UserInfo = new DataTable();
string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + fileInfo.FullName + ";" + "Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1;\"";
string strExcel = "select * from [sheet1$]";
using (OleDbDataAdapter adaptor = new OleDbDataAdapter(strExcel, strConn))
{
DataSet ds = new DataSet();
adaptor.Fill(ds);
Excel_UserInfo = ds.Tables[0];
}