Excel modify
i am trying to make this excel looks like this but not working can anyone help
https://pastecode.io/s/ew5iwzjn
101 Replies
start by not doing this
JsonSerializer.Deserialize<List<Dictionary<string, object>>>(jsonData);
make a proper model to deserialize to
you can add filters with https://github.com/ClosedXML/ClosedXML/wiki/Adding-an-AutoFilter-to-a-Rangeso i use record?
or a plain class
either works
for alternating the line colors u can do something like https://github.com/ClosedXML/ClosedXML/wiki/Using-Colors
can u $paste the json?
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
yes
or a sample of what it looks like
BlazeBin - vettabquhzcg
A tool for sharing your source code with the world!
there are some strings that are numbers
by using a option u can easily use the actual type for it
but I dont know all your data to be able to say u can use the actual type for those strings
but like the Modifiers for example
which are all in ""
will this also work
not sure if DateTime would work here I think u need DateTimeOffset but u can try
you're also not use the PlaceOfService
public List<PlaceOfService> PlaceOfServices { get; set; } its part of the ServiceRecord so its not inside it wont be deserialized
inside public class PlaceOfService
{
public int Id { get; set; }
public int DocumentId { get; set; }
public string LocCode { get; set; }
public string PlaceOfServiceName { get; set; }
}
this should i remove placeof service
if u use
u can do what I said above as in
yes u should replace it
with this
public List<PlaceOfService> PlaceOfServices { get; set; }
with
ok let me try and check if its work.
https://pastecode.io/s/ezt3vi66
i tried this but its not showing the string values in excel only data number and bool
anyone alive?
and what is your code now?
https://pastecode.io/s/3gpcgzsx
data.json
I mean yoru code is still the same
u haven't looked at all at the wiki
https://github.com/ClosedXML/ClosedXML/wiki/Data-Types
https://github.com/ClosedXML/ClosedXML/wiki/Styles-NumberFormat
I dont expect things to work just because u have a model for your json
that is just fixing 1 problem
u have to properly use the library if u want the output to be what u expect
in this code i was trying to use datetime in format or starttime endtime using datetimeoffset but i am seeing ##### in those places
I dont see that anywhere in the code
i using string datatype instead
let me show you.
pls do
also u dont need to do this
u would just use data.placeserviceL
ok but where is the code?
because the code u gave me above ur not doing anything with datetime
leowest
Quoted by
<@1102729783969861782> from #Excel modify (click here)
React with ❌ to remove this embed.
code is same i just changes it to string to DateTimeOffset or datetime
I mean you're not using closexml at all... so yeah, I dont expect it to work
read the links I send u above
ok
it shows examples on how to do things
shows how to format currency
shows how to format datetime
so i have to explicitly mention the datatype in closedxml when adding row column?
yes
if u want it to behave as that type
if u dont it also shows how u handle it
looks like i have to change alot
yep 🙂 now u see my point
since i was creating a table earlier but in code it they r using cell
well u would be working in a loop
so there are a few ways u could do it
the first one would be using Cell with column/row indexes and the other one would be using a datatable with types and adding rows
lets see
ngl i am new to c# and its a lot different from other languages
with cells u would do something like
perhaps u might want to take a trip to https://learn.microsoft.com/en-us/shows/csharp-fundamentals-for-absolute-beginners/
C# Fundamentals for Absolute Beginners
Learn C# programming from an expert in the industry. Get the tools, see how to write code, debug features, explore customizations, and more. For newer videos head over to dot.net/videos
these are short videos on the basics of c#
thanks
but yes depending on the language it would be different yes
fir header isnt it same?
the header is the top row
its often called a header because it just tells us what that column represents
so u only need to print it once
hence its outside the loop
yeah i mean i am also giving types in this
the types in the header should propagate to the rows
at least that is what the wiki says
since i have lots of headers that why
u have 1 header with many columns as I understood
but yes u dont wnat to do that
because not all of your header represents the right type u want the excel to have
like u have DateTime as string
instead of doing this i am using loop
yes you're using a loop, but your data is not properly represented
and also giving type
how isnt it same thing
public string? UtcDateCreated { get; set; }
string != DateTime
that is one example
but even if u do the datatable and resulting excel fails to show it properly then u would need to use the cell type directly to format it
but in your case your not even creating the right types with your loop
ish so i have to check every time if its a datetime then use datetime
because some of your data is not properly represented by its type
looks like i needed more time to fix it
its just a matter of checking the wiki and trying to see what works for u
and your data
thanks
like for example
in cell how do i define headers
if u want to display date time as text
as add.cloumn
it shows u that u have to append ' in front of the string
u would just use row and columns variable as indexes
and the first row, would be your header.
the cell?
what i found is i dont really need to change code just have to convert it .tostring
i.e.: something like this
new DateTime(2010, 9, 2, 13, 45, 22).ToString();
this is all in their wiki
GitHub
Home
ClosedXML is a .NET library for reading, manipulating and writing Excel 2007+ (.xlsx, .xlsm) files. It aims to provide an intuitive and user-friendly interface to dealing with the underlying OpenXM...
also the above code is hypothetical and may not work I dont remember but I think the rows and columns with excel starts at 1 instead of 0
so u might need to adjust things
ok
\
how to set the headers name
ws.Cell(row, column).DataType = XLDataType.DateTime;
Property or indexer 'IXLCell.DataType' cannot be assigned to -- it is read only
well that is on their example u would have to check why that happens
u would either skip the first row and do it outside the loop or use an if to do it on the first row
ok
well I just gave it a try and u dont even need the DataType anymore
both $"'{item.DateAsString}"; and just using item.DateTimeProperty works
how
can you share code
the problem is loop and data type since i am using a json file
no, otherwise u wont learn and will just ask people for code instead.
u dont need to set DataType
just add the data normally, Value = ...
u do however need the proper type in your json classes
because it uses that internally to define what type it is
can we do that in table not in cell
u can use the example I provided u earlier
but u need to provide the proper type in the header
well that working for me aslo but i have to click on it
in my old code
well yeah because it needs to expand
no and I already explained above why
fuck i though it was
nah when i click all of them are showing
ws.Columns(2, 6).AdjustToContents(); should fix the ###
it should expand the cell accordingly
im not sure what 2 and 6 are if its a range or a the row and column u would have to check
should i use type datetime or offset?
DateTime
apparently closedxml does not support DateTimeOffset
well some values are null its throwing error
can i also need null values in one of data and i am converting them
u have to add checks for null values
i.e.:
... guess I can't help u after all sorry.
ur back to the same code your posted at the begin
because i dont think i need to change anything
since i am specifying types in headers
its just i thought ##### was means datetime is not recognised in excel
except that everything in excel showing perfectly