Untitled

 avatar
user_0623289
csharp
2 months ago
5.1 kB
9
Indexable
Never
using ClosedXML.Excel;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text.Json;

using System;
using System.Collections.Generic;

public class Document
{
    public int DocumentId { get; set; }
    public int DocumentTemplateId { get; set; }
    public int TemplateTypeId { get; set; }
    public Guid ClientId { get; set; }
    public int ServiceId { get; set; }
    public int SiteId { get; set; }
    public string SiteName { get; set; }
    public DateTime ServiceDate { get; set; }
    public DateTime UtcDateCreated { get; set; }
    public string TemplateName { get; set; }
    public string ClientFName { get; set; }
    public string ClientMName { get; set; }
    public string ClientLName { get; set; }
    public string DateOfBirth { get; set; }
    public DateTime StartTime { get; set; }
    public DateTime EndTime { get; set; }
    public int RecordingMethodId { get; set; }
    public int ShiftId { get; set; }
    public string ShiftName { get; set; }
    public double TotalMinutes { get; set; }
    public string Service { get; set; }
    public int TotalRecords { get; set; }
    public int DocStatusId { get; set; }
    public string DocStatus { get; set; }
    public List<PlaceOfService> PlaceOfServiceList { get; set; }
    public string StaffFName { get; set; }
    public string StaffLName { get; set; }
    public string ServiceCode { get; set; }
    public string Modifier { get; set; }
    public string SecModifier { get; set; }
    public string ThirdModifier { get; set; }
    public string FourthModifier { get; set; }
    public string AgeModifier { get; set; }
    public bool IsActive { get; set; }
    public string CreatedByStaff { get; set; }
    public bool IsFaceToFace { get; set; }
    public bool IsIndirectVisit { get; set; }
    public bool IsTelehealth { get; set; }
    public object Diagnosis { get; set; }
    public string PrimaryInsurance { get; set; }
    public string ServiceRate { get; set; }
    public int NumUnits { get; set; }
    public string AmtBilled { get; set; }
    public object CustAuthId { get; set; }
    public bool IsLocked { get; set; }
    public object LockedByStaff { get; set; }
    public bool IsSigned { get; set; }
    public int CreatedBy { get; set; }
    public int ClientDiagnosisId { get; set; }
    public string ClientDiagnosisName { get; set; }
    public bool IsNoteReviewed { get; set; }
    public int BillingStatusId { get; set; }
    public string BillingStatusName { get; set; }
    public int SupervisorStaffId { get; set; }
    public object SupervisorName { get; set; }
    public DateTime PayrollDate { get; set; }
    public bool PayrollPaid { get; set; }
    public bool IsSignedByClient { get; set; }
    public bool IsSignedByReviewer { get; set; }
    public bool IsSignedByAuthor { get; set; }
    public bool IsSignedBySupervisor { get; set; }
    public int AuthId { get; set; }
    public DateTime LastBilledDate { get; set; }
    public int BatchId { get; set; }
    public int TransId { get; set; }
}

public class PlaceOfService
{
    public int Id { get; set; }
    public int DocumentId { get; set; }
    public string LocCode { get; set; }
    public string PlaceOfServiceName { get; set; }
}


class Program
{
    static void Main(string[] args)
    {
        string jsonData;
        try
        {
            jsonData = File.ReadAllText("data.json");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error reading JSON file: {ex.Message}");
            return;
        }

        var data = JsonSerializer.Deserialize<List<Document>>(jsonData);

        if (data == null || data.Count == 0)
        {
            Console.WriteLine("No data found in JSON file.");
            return;
        }

        DataTable dt = new DataTable();

        // Add columns to DataTable
        foreach (var property in typeof(Document).GetProperties())
        {
            dt.Columns.Add(property.Name);
        }

        // Add rows to DataTable and set values
        foreach (var document in data)
        {
            var newRow = dt.NewRow();
            foreach (DataColumn column in dt.Columns)
            {
                newRow[column.ColumnName] = typeof(Document).GetProperty(column.ColumnName).GetValue(document);
            }
            dt.Rows.Add(newRow);
        }

        // Save DataTable to Excel file
        try
        {
            using (var workbook = new XLWorkbook())
            {
                var worksheet = workbook.Worksheets.Add("Sheet1");
                worksheet.Cell(1, 1).InsertTable(dt);

                string fileName = "output.xlsx";
                workbook.SaveAs(fileName);

                Console.WriteLine("Excel file saved as: " + fileName);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error saving Excel file: {ex.Message}");
        }
    }
}
Leave a Comment