我正在尝试将包含两列的制表符分隔文件导出到Excel。我无法找到显示如何使用EPPlus执行此操作的示例。我见过的大多数示例都与LoadFromDatabase有关。我的猜测是读取文件并将数据存储为数组或数组列表,然后执行LoadFromArrays。
文件中的数据如下所示:
RFID评论 ABC245设备已添加到系统中 找不到BBT988设备 已售出GBN471设备
以下是我尝试编写代码以执行导出到Excel。目前,我遇到了一个投射错误并试图绕过它。我正在寻找的是获得有关编写代码以执行操作的正确有效方法的反馈。提前谢谢!
Dim fLoc As String
fLoc = Server.MapPath(".")
Dim tFile As StreamReader
tFile = File.OpenText(fileLoc)
Dim writeLn As String
writeLn = tFile.ReadToEnd
Response.Write(writeLn)
fLoc = Server.MapPath(".") & "/maindirectory" & param1 & param2
tFile = File.OpenText(fileLoc)
writeLn = ""
writeLn = tFile.ReadToEnd
writeLn = RTrim(writeLn)
Dim aList As New ArrayList
aList.Add(writeLn)
Dim attachment as String
attachment = "attachment; filename=" + "sheet.xlsx"
Dim xlPack As ExcelPackage = New ExcelPackage()
Dim ws As ExcelWorksheet = xlPack.Workbook.Worksheets.Add(sheet)
Dim ms As MemoryStream = New MemoryStream()
ws.Cells(1, 1).LoadFromArrays(aList)
xlPack.SaveAs(ms)
ms.WriteTo(Response.OutputStream)
答案 0 :(得分:1)
使用ExcelTextFormat
解析文件,如果需要,还允许为列/单元格格式设置一些基本数据类型。例如,使用制表符分隔的数据:
Header 1 Header 2
0 03/22/2017 18:03
1 03/23/2017 18:03
2 03/24/2017 18:03
3 03/25/2017 18:03
4 03/26/2017 18:03
生成Excel文件:
Dim excelTextFormat = New ExcelTextFormat()
excelTextFormat.Delimiter = vbTab
excelTextFormat.DataTypes = New eDataTypes() _
{
eDataTypes.Number, eDataTypes.DateTime
}
Using package = New ExcelPackage(New FileInfo(output))
package.Workbook.Worksheets.Add("sheet1")
Dim ws = package.Workbook.Worksheets(1)
ws.Column(2).Style.Numberformat.Format = "mm/dd/yyyy"
ws.Cells("A1").LoadFromText(input, excelTextFormat)
package.Save()
End Using
输出: