내 응용 프로그램에서는 한 CSV에서 다른 CSV 파일로 콘텐츠를 복사하고 있습니다. 이제 outfile 형식이 csv가 아닌 xls 여야한다는 것을 알게되었습니다. xls로 변환 된 복사 된 파일 또는 xls로 직접 복사하는 것이 좋습니다. 어떤 제안을 해주시겠습니까?vs.net에서 csv 파일을 XLS 파일로 변환하는 방법
1
A
답변
2
NPOI을 사용하여 .NET에서 XLS 문서를 생성합니다. 사용하기가 매우 쉽고 무료입니다.
1
EPPlus은 .NET에서 Excel 파일을 생성하기위한 좋은 오픈 소스 라이브러리입니다. CSV에서 파일을 생성하고 Excel을 만드는 것이 매우 쉽습니다.
2
다음은 가능한 구현입니다. 그것은 DataTable을에 파일에서 모든 CSV 변환하고 Google's ExcelLibrary와 XLS 파일로 (무료`의 첫 번째 프로젝트에서 DLL에 대한 참조를 추가) 것으로 변환 : 여기
Class FileHandler
Public Sub New()
End Sub
Public Sub New(ByVal sFilename As String)
FileInf = New FileInfo(sFilename)
End Sub
Public Property FileInf() As FileInfo
Get
Return m_FileInf
End Get
Set(ByVal value As FileInfo)
m_FileInf = value
End Set
End Property
Private m_FileInf As FileInfo
Private mvHeaderRow As Integer = -1
Public Property HeaderRow() As Integer
Get
Return mvHeaderRow
End Get
Set(ByVal value As Integer)
mvHeaderRow = value
End Set
End Property
Public Property DataRow1() As Integer
Get
Return m_DataRow1
End Get
Set(ByVal value As Integer)
m_DataRow1 = value
End Set
End Property
Private m_DataRow1 As Integer
Public Property Delimiter() As String
Get
Return m_Delimiter
End Get
Set(ByVal value As String)
m_Delimiter = value
End Set
End Property
Private m_Delimiter As String
Public Property MaxRows() As Integer
Get
Return m_MaxRows
End Get
Set(ByVal value As Integer)
m_MaxRows = value
End Set
End Property
Private m_MaxRows As Integer
Public Function CSVToTable() As DataTable
Try
' trap if the fileinfo has not been added to the object
If FileInf Is Nothing Then
Return Nothing
End If
Dim dtData As New DataTable()
Dim oTR As TextReader = IO.File.OpenText(FileInf.FullName)
Dim sLine As String = Nothing
Dim arData As String()
'array of strings to load the data into for each line read in
Dim drData As DataRow
Dim iRows As Integer = 0
'get the header row
If mvHeaderRow > -1 Then
For i As Integer = 0 To (mvHeaderRow + 1) - 1
sLine = CleanString(oTR.ReadLine())
Next
Else
'get the first row to count the columns
sLine = CleanString(oTR.ReadLine())
End If
'create the columns in the table
CreateColumns(dtData, sLine)
'bail if the table failed
If dtData.Columns.Count = 0 Then
Return Nothing
End If
'reset the text reader
oTR.Close()
oTR = IO.File.OpenText(FileInf.FullName)
'get the first data line
For i As Integer = 0 To (DataRow1 + 1) - 1
sLine = CleanString(oTR.ReadLine())
Next
While True
'populate the string array with the line data
arData = sLine.Split(New String() {Delimiter}, StringSplitOptions.None)
'load thedatarow
drData = dtData.NewRow()
For i As Integer = 0 To dtData.Columns.Count - 1
'test for additional fields - this can happen if there are stray commas
If i < arData.Length Then
drData(i) = arData(i)
End If
Next
'only get the top N rows if there is a max rows value > 0
iRows += 1
If MaxRows > 0 AndAlso iRows > MaxRows Then
Exit While
End If
'add the row to the table
dtData.Rows.Add(drData)
'read in the next line
sLine = CleanString(oTR.ReadLine())
If sLine Is Nothing Then
Exit While
End If
End While
oTR.Close()
oTR.Dispose()
dtData.AcceptChanges()
Return dtData
Catch Exc As Exception
Throw Exc
End Try
End Function
Private Function CleanString(ByVal sLine As String) As String
Try
If sLine Is Nothing Then
Return Nothing
End If
sLine = sLine.Replace("'", "''")
sLine = sLine.Replace("""", "")
Return sLine
Catch Exc As Exception
Throw Exc
End Try
End Function
Private Sub CreateColumns(ByVal oTable As DataTable, ByVal sLine As String)
Try
Dim oCol As DataColumn
Dim sTemp As String
Dim iCol As Integer = 0
Dim arData As String() = sLine.Split(New String() {Delimiter}, StringSplitOptions.None)
For i As Integer = 0 To arData.Length - 1
'get the header labels from the row
sTemp = String.Empty
If mvHeaderRow <> -1 Then
sTemp = arData(i)
End If
'deal with the empty string (may be missing from the row)
If (sTemp.Trim()).Length = 0 Then
sTemp = String.Format("ColName_{0}", i.ToString())
End If
'Deal with duplicate column names in the title row
iCol = oTable.Columns.Count + 100
While oTable.Columns.Contains(sTemp)
sTemp = String.Format("ColName_{0}", iCol.ToString())
End While
oCol = New DataColumn(sTemp, System.Type.[GetType]("System.String"))
oTable.Columns.Add(oCol)
Next
Catch Exc As Exception
Throw Exc
End Try
End Sub
이 방법에 대한 예입니다 그것은 작동 :
Dim ds As New DataSet("DS")
Dim dt As New DataTable("DT")
Dim handler As New FileHandler("C:\Temp\MyExcelFile.csv")
dt = handler.CSVToTable
ds.Tables.Add(dt)
ExcelLibrary.DataSetHelper.CreateWorkbook("C:\Temp\MyExcelFile.xls", ds)
에서 영감 : http://www.codeproject.com/KB/files/CSVtoTabletoCSV.aspx
관련 문제
- 1. 액세스 2000 VBA에서 xls 파일을 csv 파일로 변환하는 방법?
- 2. .xls 파일을 .dbf 파일로 변환하는 방법?
- 3. .xls 파일을 .csv 파일로 변환하는 간단한 방법이 있습니까? (Excel)
- 4. pdf 파일을 CSV 파일로 변환하는 방법은 무엇입니까?
- 5. IE6 XLS 파일을 XLS 파일로 열기
- 6. 엑셀 xls 파일을 xml 파일로 변환 자동화
- 7. VS.NET에서 빌드 파일을 생성하는 방법
- 8. Android에서 파일을 .csv 형식으로 변환하는 옵션이 있습니까?
- 9. plist 파일을 csv 파일로 변환하는 방법은 무엇입니까? (XCODE)
- 10. NSMutableArray를 iPhone에서 CSV 파일로 변환하는 방법은 무엇입니까?
- 11. .prc 파일을 .mobi 파일로 변환하는 방법
- 12. 델파이에서 HTML 파일을 pdf 파일로 변환하는 방법
- 13. Excel 파일을 SQL 파일로 변환하는 방법
- 14. ipa 파일을 deb 파일로 변환하는 방법
- 15. 엑셀 파일을 mdf 파일로 변환하는 방법
- 16. Xml 파일을 텍스트 파일로 변환하는 방법
- 17. Xml 파일을 텍스트 파일로 변환하는 방법 2
- 18. xls 파일을 mysql 형식으로 변환하는 방법은 무엇입니까?
- 19. xls 파일을 xml로 변환하는 방법은 무엇입니까?
- 20. A7에서 .xls 파일을 실행하고 .csv 파일을 만드는 방법은 무엇입니까?
- 21. .doc/.xls 파일을 Java를 사용하여 PDF로 변환하는 방법
- 22. InputStream을 가상 파일로 변환하는 방법
- 23. xls 파일을 데이터베이스에 가장 빨리 업로드하는 방법
- 24. .csv 파일을 리소스 파일로 추가하여 코드에서 액세스
- 25. PHP에서 텍스트 파일을 mysql 테이블을 변환하는 방법?
- 26. .CSV 파일을 .XML로 변환하는 PHP 스크립트
- 27. asp.net의 csv 파일로 응답
- 28. 파이썬을 사용하여 .csv 파일을 .dbf 파일로 변환 하시겠습니까?
- 29. AutocadDWG를 PDF 파일로 변환하는 방법?
- 30. 60MB CSV 파일을 재정렬하는 방법
당신이 나중에 엑셀 파일에 어떤 작업을 수행 할, 또는 그냥 유지 보수 목적을위한합니까? –
그냥 확인 : 엑셀이 CSV 파일을 XLS로 변환하지 않고 열 것이라고 알고 있습니까? – JohnFx
순수하게 유지 보수 목적으로 사용합니다. 행과 열 형식이어야합니다. – Ram