Last active
March 15, 2024 10:32
-
-
Save Vijay-Kumavat/5d51dbc2c1915954433a29cb7914df82 to your computer and use it in GitHub Desktop.
Raff C# Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Data; | |
using System.IO; | |
using ExcelDataReader; | |
class Person | |
{ | |
public string Name { get; set; } | |
public int Age { get; set; } | |
public bool IsStudent { get; set; } | |
public double GPA { get; set; } | |
} | |
class Program | |
{ | |
static void Main() | |
{ | |
string filePath = "sample.xlsx"; // Replace with your Excel file path | |
string worksheetName = "Sheet1"; // Replace with the worksheet name | |
string columnName = "Name"; // Replace with the desired column header | |
List<Person> persons = new List<Person>(); | |
using (var stream = File.Open(filePath, FileMode.Open, FileAccess.Read)) | |
{ | |
using (var reader = ExcelReaderFactory.CreateReader(stream)) | |
{ | |
// Find the column index by matching the header name | |
var columnNames = GetColumnNames(reader, worksheetName); | |
int columnIndex = Array.IndexOf(columnNames, columnName); | |
if (columnIndex == -1) | |
{ | |
Console.WriteLine($"Column '{columnName}' not found."); | |
return; | |
} | |
while (reader.Read()) | |
{ | |
string cellValue = reader.GetString(columnIndex); | |
if (!string.IsNullOrEmpty(cellValue)) | |
{ | |
persons.Add(new Person { Name = cellValue }); | |
} | |
} | |
} | |
} | |
foreach (var person in persons) | |
{ | |
Console.WriteLine($"Name: {person.Name}"); | |
} | |
} | |
// Helper function to get column names from the Excel file | |
static string[] GetColumnNames(IExcelDataReader reader, string worksheetName) | |
{ | |
var columnNames = new List<string>(); | |
reader.IsFirstRowAsColumnNames = true; | |
while (reader.Read()) | |
{ | |
for (int i = 0; i < reader.FieldCount; i++) | |
{ | |
columnNames.Add(reader.GetValue(i).ToString()); | |
} | |
break; // Read only the first row (column headers) | |
} | |
return columnNames.ToArray(); | |
} | |
} | |
using ClosedXML.Excel; | |
using Newtonsoft.Json; | |
class Program | |
{ | |
static void Main() | |
{ | |
string filePath = "sample.xlsx"; // Replace with your Excel file path | |
string worksheetName = "Sheet1"; // Replace with the worksheet name | |
// Create or open the Excel workbook | |
using (var workbook = new XLWorkbook(filePath)) | |
{ | |
// Access the worksheet by name | |
var worksheet = workbook.Worksheet(worksheetName); | |
// Find the column index for the 'Address' column | |
int addressColumnIndex = worksheet.ColumnsUsed().First(c => c.Cell(1).Value.ToString() == "Address").ColumnNumber(); | |
// Iterate through the rows | |
int startRow = 2; // Assuming data starts from row 2 | |
int rowCount = worksheet.RowCount(); | |
for (int row = startRow; row <= rowCount; row++) | |
{ | |
var cell = worksheet.Cell(row, addressColumnIndex); | |
string jsonAddress = cell.Value.ToString(); | |
// Deserialize the JSON string into the 'Address' class | |
Address address = JsonConvert.DeserializeObject<Address>(jsonAddress); | |
// Access the nested class properties | |
string street = address.Street; | |
string city = address.City; | |
Console.WriteLine($"Row {row}: Street: {street}, City: {city}"); | |
} | |
} | |
} | |
} | |
using ClosedXML.Excel; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
class Program | |
{ | |
static void Main() | |
{ | |
string filePath = "sample.xlsx"; // Replace with your Excel file path | |
string worksheetName = "Sheet1"; // Replace with the worksheet name | |
List<Dictionary<string, object>> data = new List<Dictionary<string, object>>(); | |
using (var workbook = new XLWorkbook(filePath)) | |
{ | |
var worksheet = workbook.Worksheet(worksheetName); | |
// Find the nested headers | |
var headers = FindNestedHeaders(worksheet); | |
foreach (var row in worksheet.RowsUsed().Skip(headers.Count)) | |
{ | |
var rowData = new Dictionary<string, object>(); | |
for (int i = 1; i <= row.LastCellUsed().Address.ColumnNumber; i++) | |
{ | |
string columnName = headers[i - 1]; | |
var cell = row.Cell(i); | |
rowData[columnName] = cell.Value; | |
} | |
data.Add(rowData); | |
} | |
} | |
// Convert the data to JSON or perform further processing as needed | |
// ... | |
foreach (var item in data) | |
{ | |
Console.WriteLine("Row Data:"); | |
foreach (var kvp in item) | |
{ | |
Console.WriteLine($" {kvp.Key}: {kvp.Value}"); | |
} | |
Console.WriteLine(); | |
} | |
} | |
static List<string> FindNestedHeaders(IXLWorksheet worksheet) | |
{ | |
var headers = new List<string>(); | |
var firstRow = worksheet.Row(1); | |
foreach (var cell in firstRow.CellsUsed()) | |
{ | |
var headerPath = new List<string>(); | |
var currentCell = cell; | |
while (currentCell.HasData) | |
{ | |
headerPath.Add(currentCell.GetString()); | |
currentCell = currentCell.CellBelow(); | |
} | |
string header = string.Join(" > ", headerPath); | |
headers.Add(header); | |
} | |
return headers; | |
} | |
} | |
using ClosedXML.Excel; | |
using System; | |
using System.Collections.Generic; | |
class Program | |
{ | |
static void Main() | |
{ | |
string filePath = "sample.xlsx"; // Replace with your Excel file path | |
string worksheetName = "Sheet1"; // Replace with the worksheet name | |
List<Person> people = new List<Person>(); | |
using (var workbook = new XLWorkbook(filePath)) | |
{ | |
var worksheet = workbook.Worksheet(worksheetName); | |
foreach (var row in worksheet.RowsUsed()) | |
{ | |
int id = row.Cell("A").GetValue<int>(); | |
string name = row.Cell("B").GetValue<string>(); | |
int age = row.Cell("C").GetValue<int>(); | |
bool isStudent = row.Cell("D").GetValue<bool>(); | |
var person = new Person | |
{ | |
ID = id, | |
Name = name, | |
Age = age, | |
IsStudent = isStudent | |
}; | |
people.Add(person); | |
} | |
} | |
foreach (var person in people) | |
{ | |
Console.WriteLine($"ID: {person.ID}, Name: {person.Name}, Age: {person.Age}, IsStudent: {person.IsStudent}"); | |
} | |
} | |
} | |
using NPOI.HSSF.UserModel; // For .xls files | |
using NPOI.SS.UserModel; | |
using System; | |
using System.IO; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string filePath = "your_excel_file.xls"; // Replace with your XLS file path | |
try | |
{ | |
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) | |
{ | |
IWorkbook workbook = new HSSFWorkbook(fs); // For .xls files | |
// Choose the worksheet you want to read (e.g., the first sheet) | |
ISheet sheet = workbook.GetSheetAt(0); | |
for (int rowIndex = 0; rowIndex <= sheet.LastRowNum; rowIndex++) | |
{ | |
IRow row = sheet.GetRow(rowIndex); | |
if (row != null) | |
{ | |
for (int cellIndex = 0; cellIndex < row.LastCellNum; cellIndex++) | |
{ | |
ICell cell = row.GetCell(cellIndex); | |
if (cell != null) | |
{ | |
Console.Write(cell.ToString() + "\t"); | |
} | |
} | |
Console.WriteLine(); // Move to the next row | |
} | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine("Error: " + ex.Message); | |
} | |
} | |
} | |
using OfficeOpenXml; | |
// Replace "your-excel-file.xls" with the path to your Excel file | |
FileInfo fileInfo = new FileInfo("your-excel-file.xls"); | |
using (ExcelPackage package = new ExcelPackage(fileInfo)) | |
{ | |
// Select the worksheet you want to read from | |
ExcelWorksheet worksheet = package.Workbook.Worksheets[0]; // 0 is the index of the first worksheet | |
int rowCount = worksheet.Dimension.Rows; | |
for (int row = 1; row <= rowCount; row++) | |
{ | |
// Read each cell in the current row | |
for (int col = 1; col <= worksheet.Dimension.Columns; col++) | |
{ | |
var cellValue = worksheet.Cells[row, col].Text; | |
// Do something with the cell value, e.g., print it | |
Console.Write(cellValue + "\t"); | |
} | |
// Move to the next line after reading a row | |
Console.WriteLine(); | |
} | |
} | |
System.IO.InvalidDataException: 'The file is not a valid Package file. If the file is encrypted, please supply the password in the constructor.' | |
using (var stream = File.Open("your-excel-file.xls", FileMode.Open, FileAccess.Read)) | |
{ | |
using (var reader = ExcelReaderFactory.CreateReader(stream)) | |
{ | |
do | |
{ | |
while (reader.Read()) | |
{ | |
for (int i = 0; i < reader.FieldCount; i++) | |
{ | |
// Read each cell in the current row | |
Console.Write(reader[i] + "\t"); | |
} | |
Console.WriteLine(); | |
} | |
} while (reader.NextResult()); | |
} | |
} | |
NPOI.POIFS.FileSystem.NotOLE2FileException: 'Invalid header signature; read 0x6D78206C6D74683C, expected 0xE11AB1A1E011CFD0 - Your file appears not to be a valid OLE2 document' | |
using OfficeOpenXml; | |
using OfficeOpenXml.Table; | |
using System.IO; | |
// Load the XLS file | |
using (FileStream xlsFileStream = new FileStream("input.xls", FileMode.Open, FileAccess.Read)) | |
{ | |
using (var xlsPackage = new ExcelPackage(xlsFileStream)) | |
{ | |
var xlsWorksheet = xlsPackage.Workbook.Worksheets[0]; // Assuming you want to convert the first sheet | |
// Create a new XLSX package in memory | |
using (var xlsxPackage = new ExcelPackage()) | |
{ | |
var xlsxWorksheet = xlsxPackage.Workbook.Worksheets.Add(xlsWorksheet.Name); | |
// Copy data from the XLS sheet to the XLSX sheet | |
for (int row = 1; row <= xlsWorksheet.Dimension.End.Row; row++) | |
{ | |
for (int col = 1; col <= xlsWorksheet.Dimension.End.Column; col++) | |
{ | |
xlsxWorksheet.Cells[row, col].Value = xlsWorksheet.Cells[row, col].Value; | |
} | |
} | |
// Read from the XLSX package in memory | |
for (int row = 1; row <= xlsxWorksheet.Dimension.End.Row; row++) | |
{ | |
for (int col = 1; col <= xlsxWorksheet.Dimension.End.Column; col++) | |
{ | |
var cellValue = xlsxWorksheet.Cells[row, col].Text; // Get the value of each cell | |
// Process the cell value here | |
} | |
} | |
} | |
} | |
} | |
using ClosedXML.Excel; | |
using System.IO; | |
// Open the XLSX file | |
using (var workbook = new XLWorkbook("input.xlsx")) | |
{ | |
// Select the worksheet you want to read | |
var worksheet = workbook.Worksheet(1); // Assuming you want to read the first sheet | |
// Iterate through rows, starting from the 7th row | |
for (int row = 7; row <= worksheet.RowCount(); row++) | |
{ | |
// Initialize a variable to store the values in the row | |
List<string> rowData = new List<string>(); | |
// Iterate through columns in the row | |
for (int col = 1; col <= worksheet.ColumnCount(); col++) | |
{ | |
var cell = worksheet.Cell(row, col); | |
string cellValue = cell.GetString(); | |
// Add the cell value to the row data | |
rowData.Add(cellValue); | |
} | |
// Process the row data as needed | |
// rowData contains the values in the current row, starting from the 7th row | |
} | |
} | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
class Program | |
{ | |
static void Main() | |
{ | |
List<int> list1 = new List<int> { 1, 2, 3, 4, 5 }; | |
List<int> list2 = new List<int> { 2, 3, 5, 6, 7 }; | |
// Find the differences between the two lists with indices. | |
List<Difference<int>> differences = FindDifferencesWithIndices(list1, list2); | |
// Display the differences. | |
foreach (var diff in differences) | |
{ | |
Console.WriteLine($"Value: {diff.Value}, Index in List1: {diff.IndexInList1}, Index in List2: {diff.IndexInList2}"); | |
} | |
} | |
static List<Difference<T>> FindDifferencesWithIndices<T>(List<T> list1, List<T> list2) | |
{ | |
var differences = new List<Difference<T>>(); | |
for (int i = 0; i < list1.Count; i++) | |
{ | |
if (i < list2.Count) | |
{ | |
if (!EqualityComparer<T>.Default.Equals(list1[i], list2[i])) | |
{ | |
differences.Add(new Difference<T> { Value = list1[i], IndexInList1 = i, IndexInList2 = i }); | |
} | |
} | |
else | |
{ | |
differences.Add(new Difference<T> { Value = list1[i], IndexInList1 = i, IndexInList2 = -1 }); | |
} | |
} | |
for (int i = list1.Count; i < list2.Count; i++) | |
{ | |
differences.Add(new Difference<T> { Value = list2[i], IndexInList1 = -1, IndexInList2 = i }); | |
} | |
return differences; | |
} | |
} | |
class Difference<T> | |
{ | |
public T Value { get; set; } | |
public int IndexInList1 { get; set; } | |
public int IndexInList2 { get; set; } | |
} | |
var differenceWithIndices = list1.Select((item, index) => new { Item = item, Index = index }) | |
.Join(list2.Select((item, index) => new { Item = item, Index = index }), | |
item1 => item1.Item, | |
item2 => item2.Item, | |
(item1, item2) => new { Index1 = item1.Index, Index2 = item2.Index }) | |
.ToList(); | |
var differingIndices = list1 | |
.Select((value, index) => new { Value = value, Index = index }) | |
.Where(item => !list2.Contains(item.Value)) | |
.Select(item => item.Index) | |
.ToList(); | |
using ceTe.DynamicPDF; | |
using ceTe.DynamicPDF.LayoutEngine; | |
class Program | |
{ | |
static void Main() | |
{ | |
// Create a document and a page | |
Document document = new Document(); | |
Page page = new Page(); | |
// Create a LayoutArea and add it to the page | |
LayoutArea layoutArea = new LayoutArea(page, 20, 20, 550, 700); | |
// Create a LayoutElement with some content | |
LayoutElement layoutElement = new LayoutElement(); | |
layoutElement.Height = 200; // Set the height of the element | |
// Set the PageBreaking property to true to allow page breaks | |
layoutElement.PageBreaking = true; | |
// Add the LayoutElement to the LayoutArea | |
layoutArea.Add(layoutElement); | |
// Add the LayoutArea to the page | |
page.LayoutAreas.Add(layoutArea); | |
// Add the page to the document | |
document.Pages.Add(page); | |
// Save the document | |
document.Draw("output.pdf"); | |
} | |
} | |
var client = new HttpClient(); | |
var request = new HttpRequestMessage(HttpMethod.Post, "https://paycorreportgenerationservicequarterly.trafficmanager.net/api/QueueReport"); | |
request.Headers.Add("Authorization", "Bearer 24Qu-WThRPNCyKIZGhtIgx7Sc3thmhsC7DH-cEWqnYDYXs7ln_YH39ML_DOLFpiCT4RDz0Lpo7NKNHLs4fzyU1Qmfrulc93smW-YZFkw6cS0RCdG89B3c0dkTq001f5hPR_eKDs111dY3QnjolJJRm0twoYyo_yq5jRP1H5yPUFj9pS0"); | |
var content = new StringContent("{\n \"clientId\": 276516,\n \"pquid\": 183038496563790,\n \"payrollUid\": 20382076723788,\n \"processDate\": \"2024-03-12\",\n \"checkDate\": \"2024-03-13\",\n \"plannerUid\": 271614518632822,\n \"reportsType\": \"CPR\",\n \"runType\": \"A\",\n \"manualRunGenerationType\": 2\n}", null, "application/json"); | |
request.Content = content; | |
var response = await client.SendAsync(request); | |
response.EnsureSuccessStatusCode(); | |
Console.WriteLine(await response.Content.ReadAsStringAsync()); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment