using System.Linq;
using LinqToExcel;
using System.IO;
private void ProcessExcelToFiles( )
{
// Open and query the Excel document. This string, "C:\Working Directory\testExcel.xlsx", in the next line is the path and filename for the Excel file.
var excel = new ExcelQueryFactory(@"C:\Working Directory\testExcel.xlsx");
// In the next line change "Sheet1" in the next line to the name of the sheet name where the data is in.
var lines = (from c in excel.Worksheet("Sheet1")
select c).ToList();
// The number of files that have been created through each loop.
int fileIndex = 1;
// Number of rows in each file except maybe the last
int linesPerFile = 50;
// Total lines to process after each loop
int totalLinesRemaining = lines.Count();
// Used for paggination, point in the list the last line that was processed
int skip = 0;
// When done processing line make this false
bool notDone = true;
// Number of columns in the excel file.
int numberOfColumns = lines[0].ColumnNames.Count();
while( notDone )
{
// Create the file name, true part full lines per file, false part number of line remaining
int intFilename = (totalLinesRemaining / linesPerFile > 0
? fileIndex * linesPerFile
: ((lines.Count() / linesPerFile) * linesPerFile) + (totalLinesRemaining % linesPerFile));
// Keep track of the number of line processed
totalLinesRemaining -= linesPerFile;
fileIndex += 1;
// Are we done
if (totalLinesRemaining <= 0) notDone = false;
// Create filename as a string from integer.
string filename = intFilename + ".csv";
// Write data to the files.
using( StreamWriter sw = new StreamWriter(filename) )
{
// Get the lines to write from the list, skipping over the lines already processed
var linesToWrite = (from l in lines.Skip(skip).Take(linesPerFile)
select l).ToList();
// Get ready for the next lines
skip += linesPerFile;
// Write the headers to the file.
string headers = lines[0].ColumnNames.Aggregate((output, name) => output + ", " + name);
sw.WriteLine(headers);
// Now write the data for each row to the file.
foreach (Row row in linesToWrite)
{
List<string> rowValues = new List<string>();
row.ForEach(cell => rowValues.Add(cell.ToString()));
sw.WriteLine(rowValues.Aggregate((output, value) => output + ", " + value));
}
}
}
}
using System.Linq;
using LinqToExcel;
using System.IO;
Public Sub SplitXLForEach50Row()
' Solution for question ID 28656054 by ProfessorJimJam
Dim inputFile As String, inputWB As Workbook
Dim lastRow As Long, row As Long, n As Long
Dim newCSV As Workbook
inputFile = [u][b]"C:\Users\Split.xlsx"[/b][/u]
Set inputWB = Workbooks.Open(inputFile)
With inputWB.Worksheets(1)
lastRow = .Cells(Rows.Count, "A").End(xlUp).row
Set newCSV = Workbooks.Add
nNumber = 0
For row = 2 To lastRow Step 50
nNumber = nNumber + 50
.Rows(1).EntireRow.Copy newCSV.Worksheets(1).Range("A1")
.Rows(row & ":" & row + 50 - 1).EntireRow.Copy newCSV.Worksheets(1).Range("A2")
'Saves in same folder as input workbook with .xlsx
newCSV.SaveAs Filename:=Replace(inputWB.FullName, ".xlsx", "(" & nNumber & ").csv"), FileFormat:=xlCSV, CreateBackup:=False
Next
End With
newCSV.Close saveChanges:=False
inputWB.Close saveChanges:=False
End Sub
Public Sub SplitXLForEach50Row()
' Solution for question ID 28656054 by ProfessorJimJam
Dim inputFile As String, inputWB As Workbook
Dim lastRow As Long, row As Long, n As Long
Dim newCSV As Workbook
inputFile = "C:\Users\Split.xlsx" ' modify to your desired path location and file name
Set inputWB = Workbooks.Open(inputFile)
With inputWB.Worksheets(1)
lastRow = .Cells(Rows.Count, "A").End(xlUp).row
Set newCSV = Workbooks.Add
nNumber = 0
For row = 2 To lastRow Step 50
nNumber = nNumber + 50
.Rows(1).EntireRow.Copy newCSV.Worksheets(1).Range("A1")
.Rows(row & ":" & row + 50 - 1).EntireRow.Copy newCSV.Worksheets(1).Range("A2")
'Saves in same folder as input workbook with .xlsx
newCSV.SaveAs Filename:=Replace(inputWB.FullName, ".xlsx", "(" & nNumber & ").csv"), FileFormat:=xlCSV, CreateBackup:=False
Next
End With
newCSV.Close saveChanges:=False
inputWB.Close saveChanges:=False
End Sub
Open in new window