c#如何使用ExcelPackage Plus读取excel(.xls和xlsx)中得数据
答案:1 悬赏:40 手机版
解决时间 2021-04-15 18:34
- 提问者网友:疯子也有疯子的情调
- 2021-04-15 14:11
c#如何使用ExcelPackage Plus读取excel(.xls和xlsx)中得数据
最佳答案
- 五星知识达人网友:时间的尘埃
- 2021-04-15 14:19
ExcelPackage Plus是ExcelPackage的增强版,它在ExcelPackage的基础上,增强了许多功能包括对流、Linq的支持,可以说相当不错。
它的索引号是从1开始的,主要使用的类型位于OfficeOpenXml空间,具体测试代码如下:
MemoryStream CreateByExcelLibrary(DataTable table)
{
using(ExcelPackage package = newExcelPackage())
{
ExcelWorksheet sheet = package.Workbook.Worksheets.Add("sheet111"); intcolCount = table.Columns.Count;
for(inti = 0; i < colCount; i++)
{
sheet.Cells[1, i + 1].Value = table.Columns[i].Caption;
}
intk = 2;
foreach(DataRow row intable.Rows)
{
for(inti = 0; i < colCount; i++)
{
sheet.Cells[k, i + 1].Value = row[i];
}
k++;
}
MemoryStream ms = newMemoryStream();
package.SaveAs(ms);
returnms;
} }
DataTable ReadByExcelLibrary(Stream xlsStream)
{
DataTable table = newDataTable();
using(ExcelPackage package = newExcelPackage(xlsStream))
{
ExcelWorksheet sheet = package.Workbook.Worksheets[1];
intcolCount = sheet.Dimension.End.Column;
introwCount = sheet.Dimension.End.Row;
for(ushortj = 1; j <= colCount; j++)
{
table.Columns.Add(newDataColumn(sheet.Cells[1, j].Value.ToString()));
}
for(ushorti = 2; i <= rowCount; i++)
{
DataRow row = table.NewRow();
for(ushortj = 1; j <= colCount; j++)
{
row[j - 1] = sheet.Cells[i, j].Value;
}
table.Rows.Add(row);
}
}
returntable;
}追问这段代码我也找到了,
using (ExcelPackage package = new ExcelPackage()) {}这句就是错得把,要求有参数得。有其他的例子么?追答知道错误了,就设断点调试下。这样会记忆深刻的。。。。。。。。。不断的尝试、不断的失败,那最后的成功才会越值得满足。。。。。追问好吧,只是我还看的有点懵,是我研究得不够透彻把。你说话好像我领导呀
它的索引号是从1开始的,主要使用的类型位于OfficeOpenXml空间,具体测试代码如下:
MemoryStream CreateByExcelLibrary(DataTable table)
{
using(ExcelPackage package = newExcelPackage())
{
ExcelWorksheet sheet = package.Workbook.Worksheets.Add("sheet111"); intcolCount = table.Columns.Count;
for(inti = 0; i < colCount; i++)
{
sheet.Cells[1, i + 1].Value = table.Columns[i].Caption;
}
intk = 2;
foreach(DataRow row intable.Rows)
{
for(inti = 0; i < colCount; i++)
{
sheet.Cells[k, i + 1].Value = row[i];
}
k++;
}
MemoryStream ms = newMemoryStream();
package.SaveAs(ms);
returnms;
} }
DataTable ReadByExcelLibrary(Stream xlsStream)
{
DataTable table = newDataTable();
using(ExcelPackage package = newExcelPackage(xlsStream))
{
ExcelWorksheet sheet = package.Workbook.Worksheets[1];
intcolCount = sheet.Dimension.End.Column;
introwCount = sheet.Dimension.End.Row;
for(ushortj = 1; j <= colCount; j++)
{
table.Columns.Add(newDataColumn(sheet.Cells[1, j].Value.ToString()));
}
for(ushorti = 2; i <= rowCount; i++)
{
DataRow row = table.NewRow();
for(ushortj = 1; j <= colCount; j++)
{
row[j - 1] = sheet.Cells[i, j].Value;
}
table.Rows.Add(row);
}
}
returntable;
}追问这段代码我也找到了,
using (ExcelPackage package = new ExcelPackage()) {}这句就是错得把,要求有参数得。有其他的例子么?追答知道错误了,就设断点调试下。这样会记忆深刻的。。。。。。。。。不断的尝试、不断的失败,那最后的成功才会越值得满足。。。。。追问好吧,只是我还看的有点懵,是我研究得不够透彻把。你说话好像我领导呀
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯