Excel to HTML Table using C# [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Is it possible to convert an excel spreadsheet to html code, and if so what would be the best way to do so (basic code to give an idea of how to handle things)? I'm creating a web app that allows a user to upload a spread sheet and output the html code to be copied into a CMS using clean code.
Note: I'm not looking for a full code example or anything, just something to give me the right idea of how to approach this problem.
UPDATE: The answer below accurately sums up what I'm looking for. Pretty much open the file in memorystream and then format everything into a table.

In my opinion the most clean and robust solution would be processing an xlsx file in your own method and then returning string with HTML. There's very pleasant package called EPPlus, which allows you to easily manage xlsx files. You can easily iterate through columns and rows and generate HTML in any shape you want.
It could look something like that:
public string XlsxToHTML(byte[] file)
{
MemoryStream stream = new MemoryStream(file);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("<table>");
using(ExcelPackage excelPackage = new ExcelPackage(stream))
{
ExcelWorkbook workbook = excelPackage.Workbook;
if(workbook!=null)
{
ExcelWorksheet worksheet = workbook.Worksheets.FirstOrDefault();
if(worksheet!=null)
{
var firstCell = worksheet.Cells[1,1].Value;
var secondCell = worksheet.Cells[1,2].Value;
stringBuilder.Append("<tr><td>"+firstCell+"</td></tr>");
stringBuilder.Append("<tr><td>"+firstCell+"</td></tr>");
}
}
}
stringBuilder.Append("</table>");
return stringBuilder.ToString();
}
Of course it's merely an example. It's dirty because it appends tags and so on, but it's a quick example of the idea...

Related

How to read CSV file from our computer using .net core web api [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last month.
Improve this question
Some time we need to import or insert all the CSV files located in a directory into database. The following C# code will insert all the files in a directory into database: const string CSV_CONNECTIONSTRING = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="{0}";Extended Properties="text;HDR=YES;FMT=Delimited""
As we don't have a lot ot information, we just can give you some tips in order to achieve what you want:
You need to install a NuGet package that provides CSV reading functionality. One sample of it it's "CsvHelper library" CSV Library
Then just use it, you will in the docs how to use it, but one sample of it would be:
void Main()
{
using (var reader = new StreamReader("path\\to\\file.csv"))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
var records = csv.GetRecords<dynamic>();
foreach (var record in records) {
// whatever u want
}
}
}
Hope that helps.

C# - What is the best way to export the content of a list in an external file? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I'm developing a program to manage my coin collection. I'd like to export my list of coins in an external file in order to save what I've stored inside the list. As the title says, I'd like to know what is the best way to do that. Should I export the content in a text file, in a Excel file or in a XML file?
I don't know if it is useful to know this, but I'm using LINQ to manage the queries.
At the moment, everything is working as intended. The only thing that I need to finish the project is to save all the data inside the list. I'm not asking for some code to paste, I just want some opinions.
Thanks in advance for the help.
Wow, so many options!
I think these days I prefer JSON. It is lightweight simple, human readable and portable.
With a library such as Newtonsoft then it is also easy. I know you didn't ask for a code example, but below shows how easy it is.
string output = JsonConvert.SerializeObject(myObject, Newtonsoft.Json.Formatting.Indented);
File.WriteAllText("c:\path\outputfile.json", output);
And to read it in again
string json = File.ReadAllText("c:\path\inputfile.json");
MyObject myObject = JsonConvert.DeserializeObject<MyObject >(json);
And if you did want XML, you can use the sample library to then convert your obejct to XML for output
System.Xml.XmlDocument doc = JsonConvert.DeserializeXmlNode(json, "RootElementName");
doc.Save("c:\path\outputfile.xml");

Programmatically tag an untagged pdf with iTextSharp [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I want to parse a PDF file with iTextSharp. With tagged PDFs I used TaggedPdfReaderTool and the .ConvertToXml() method which returned me an acceptable XML for further parsing. Now I have to parse untagged PDFs which I need to parse! In Adobe Acrobat you can add tags to a PDF using their accessibility tool. After that I'm able to parse it with iTextSharp. Now I'm looking for a free solution to add tags to my PDF programmatically (with iTextSharp). In the book "iText in Action" I've read that you can create a tagged PDF (from an XML file) but i need to convert an existing one!
My code for parsing tagged PDFs:
var path = #"C:\Users\xxx\Desktop\xxx.pdf";
var fs = new FileStream(#"C:\Users\xxx\Desktop\xxx_tagged.xml", FileMode.Create);
PdfReader reader = new PdfReader(path);
TaggedPdfReaderTool tool = new TaggedPdfReaderTool();
tool.ConvertToXml(reader, fs);
fs.Close();
This is not an easy problem. Since you are essentially asking for a solution to do structure recognition.
Think about it. You want to know where paragraphs begin and end, you'd need a solution for figuring out tables and lists. Not to mention nested tables and lists and combinations thereof.
This is the topic of research. One popular approach is to use neural networks (treating the pdf as an image and tackling it as an image recognition task), or alternatively tackle it in a rule-based fashion.

Read .Exe file Using c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
In c# How many way to open file? Which one is best? and how to open .exe file? Sorry for silly question but i am new in c#.
using (StreamReader srStreamReader = new StreamReader(sString))
{
while ((sline = srStreamReader.ReadLine()) != null)
{
Console.WriteLine(sline);
}
}
I am use this code for this but am not able. so please help
If I understood the problem correctly
You can use like this
string path;
byte[] bufferArray = File.ReadAllBytes(path);
string base64EncodedString = Convert.ToBase64String(bufferArray );
bufferArray = Convert.FromBase64String(base64EncodedString );
File.WriteAllBytes(path, bufferArray );
By open file do you mean execute it or read line by line?
If execute then probably something like this is the answer:
Process.Start("C:\\");
From the code you've provided, it looks like you want to be able to view the source of an .exe. This can't be done without using a decompiler and knowing what the application was compiled with.
If you're trying to execute the .exe file, then take a look at the static method System.Diagnostics.Process.Start(filePath).
If you're trying to actually read the contents, you can use ILSpy or other similar software to decompile the application to view source. ILSpy has source available on GitHub, so you'll be able to use that to get the contents you want.

Write to txt in a 'form-like' manner [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm implementing a Windows application to make planning projects easier at my workplace and I was wondering if there's any clever way of making a txt-file nicely structured.
The application is really very simple, pretty much what it does is give the user a question which is answered in a textbox bellow. The question AND the answer are then both sent to a file but it looks very tacky.
Example:
Question?Answer!Question?Answer!
I would like it to be more like this:
Question?
Answer!
Question?
Answer!
I was also curious about other types files, is it possible to use Pdf or MS word the same way as txt?
You can use File.AppendAllLines() and pass in the different strings as an array. They will appear as separate lines in the text file. You'll also need to add a using System.IO at the top of your file.
For example:
// Ensure file exists before we write
if (!File.Exists("<YOUR_FILE_PATH>.txt"))
{
using (File.CreateText("<YOUR_FILE_PATH>.txt")) {}
}
File.AppendAllLines("<YOUR_FILE_PATH>.txt", new string[] {
"Question1",
"Answer1",
"Question2",
"Answer2",
"Question3",
"Answer3"
});
I hope this is what you're after - the question is a little vague.
As for Word and PDF files, this is more complex. Here's a link to a StackOverflow question about Word:
How can a Word document be created in C#?
and one about PDF:
Creating pdf files at runtime in c#
For a simple text file you could use
StringBuilder fileData = new StringBuilder();
fileData.AppendLine("Question: blah! blah! blah! blah!");
fileData.AppendLine("Answer: blah! blah! blah! blah!");
FileStream fs = new FileStream("yourFile.txt", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.Write(fileData.ToString());
sw.Flush();
fs.Flush();
fs.Close();
But of course it won't give you that bold question flavor, for that you would have to use something else,
like MS Word interop, to learn that visit here.
I wanted to mention the String.Format function.
suppose, you have your strings question and answer, you could do some
using (var stream = new StreamWriter('myfile.txt', true)) // append=true
stream.Write(String.Format("\n{0}\n{1}\n\n{2}\n",
question,
new String('=',question.Length),
answer);
to get a text file like
Question 1
==========
Answer
Second Question
===============
Answer
You might also want to use String.Trim() on question to get rid of leading and trailing whitespace (question = question.Trim()), so the "underline" effect looks nicely.

Categories