Using a csv file in Visual Studio C# - c#

I have some data saved in .csv format. Basically I want to access this data within my Visual Studio project (using C#).
I've done some digging around and found things like this:
http://www.codeproject.com/Articles/415732/Reading-and-Writing-CSV-Files-in-Csharp
But it just seems a little too complicated. Is there a **built-in method to read the csv file format within Visual Studio? If not I will start using the method shown there.
Thanks!
EDIT: Sorry perhaps I should be a little clearer. The csv file is huge (35,000 rows), I guess I should be asking how best to handle this data. Should I write it as a table, or read from the file each time I want to access the data?
If it sounds like I'm confusing terminology I'm sorry. I'm not all that familiar with C# and visual studio.

The library I've had good sucess with can be found as a download at CodeProject
http://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader
It provides a great deal of flexibility and has the ability to allow you to deal with malformed CSVs

Related

Importing a MS-Project mpp file into C# .Net Framework and exporting the data into an SQLite database using MPXJ

**
Hello
**
I'm creating a scheduling app that takes in 2 MS-Project .mpp files (master and updated) and converts the data into SQLite tables then compares them both and displays the results and allows you to write the changes you make back to the master file. I had issues with Microsoft Interop because I don't own Microsoft Project. Is MPXJ a viable solution? The documentation I've read on it doesn't have many examples. If so how would I read it in and read it back? Were using MS-project 2016
I know nothing of MPXJ, so sorry if I overlook a more straightforward answer. It sounds to me like another way of looking at your problem is you want to:
1. Parse a MS Project file (and then do it again) and store results in memory
2. Do some data manipulation and calculations of the in-memory project data
3. Put that data into a database
I think you're stuck at step 1 because without MS Project, you lack a parser; correct? There are other ways to parse a project file. The simplest may be to have your users first convert the files to a more open format (e.g. XML) when they save them from their instances of MS Project. Lacking that, there are certainly libraries out there that can parse a Project file. Try taking a look at Gantt Project, https://sourceforge.net/projects/ganttproject/ . Being open source, you could look at that parser as a starting point; I'm not a license expert, but you may even be able to re-use the code from there.
Good luck!

Importing 2 .txt files to sql server database using asp.net

I have 2 .txt files with data that i need to import to a sql server database in order to continue my project in Visual Studio C#. I was told to use the Stream writer/reader. Can someone explain to me how to use it, and show me all the aspects of how to do it? I am very new.
If you just need to insert the information by hand, you can follow the tips provided here.
If you want to do it through C#, here are some links to get you started:
First you need to parse the text files to retrieve the data. Here's an example on how to do that.
Next you'll need to insert the information. Here's a Beginners guide to doing that.
Good luck!
I have an example here.
http://granadacoder.wordpress.com/2009/01/27/bulk-insert-example-using-an-idatareader-to-strong-dataset-to-sql-server-xml/
It was written for VS2003, but updating it to VS2010 or (or 2008 or other) would be trivial.
It does not use a stream reader. It uses OleDb.
The above is a good solution if you need to do any validations on the data before inserting it.
Here is another idea.
http://www.codeproject.com/Articles/27802/Using-OleDb-to-Import-Text-Files-tab-CSV-custom

How do I generate a PDF/Excel file from an SQL database using C# and MVC 2?

I need to make the information in the database usable by allowing the user to download it as a PDF or Excel spreadsheet (either one works, both is perfect).
I've looked around at a bunch of options, but I really can't decide which one I should use, let alone if any of those options are actually useful. Most of the options I've found revolve around converting already existing HTML files into PDFs which is not what I need. Also, it needs to be free. My bosses haven't given me a budget to spend on this
I'm not sure what other information I should include here.
Well, any help is greatly appreciated. If you have questions about missing information, I'll get it posted ASAP. I'm here all day, so I'll be able to respond to any comments very quickly.
EDIT: Oh wow! Huge thanks, guys, for the massive response! I got a ton of ideas. This is super-helpful. Thanks!
if you want to generate an Excel (or also a Word) you can use openXml. You can create a new document exactly the way you want from pure code.
OpenXml SDK page
The solution I usually propose to my clients in this situation is to use Sql Server Reporting Services (SSRS). You can use the ReportViewer control included with it in order to generate PDF's, Excel spreadsheets, XML files, CSV files, and others. If you need ad hoc reporting, there is a Report Builder available as well.
Barring that, you can use OpenXml to generate Excel spreadsheets and there are a host of PDF toolkits available.
Have you looked into the reportviewer control, which is part of Visual Studio?
It allows you to export the report in PDF or Excel format.
http://www.carlosag.net/tools/excelxmlwriter/sample
check this might be useful for you
There are lots of reporting solutions out there such as SQL Server Reporting Services(for which you might already have a license). Take a look at Reporting (free || open source) Alternatives to Crystal Reports in Winforms which can likely be applied to the web with a bit of serialization.
I would suggest thinking about rolling your own depending on the situation. You could use pdfsharp for the pdf export and EPPlus for excel. They are both very easy to use and, I'm pretty sure, available in nuget with a couple of clicks.
If you want to go the Excel route, i'd recommend this article from Stephen Walther entitled ASP.NET MVC Tip #2 - Create a custom Action Result that returns Microsoft Excel Documents. This uses an old trick of writing an HTML document with an Excel mime type. This is different than streaming a native Excel file. And it's fairly easy to change the to rendering a CSV file if you want to strip it down, and make it a more universal file. Just remember to double-quote all the fields if there's a possibility of commas showing up.
If what your doing isn't too complicated you can use CSV files. CSV stands for comma separated values, and it is what it sounds like. You can create simple tables and columns using commas. For example paste the following lines into a text file:
heading1,heading2,heading3
info1,info2,info3
info1,info2,info3
Save the text file as a .csv file and voila - an excel spreadsheet. Obviously it is extremely easy to build these looping object collections. Mind you if you need any complicated text formatting etc then it is not really the best option.

Is there a code generation tool that accepts Excel files as input?

I'm looking to generate some C# code based on Excel data. Basically the Excel sheet contains definitions of functions that need to be generated in C#.
My current experience with code generation is mainly with ORM's. But now the "input" is Excel data.
I'm asking this to prevent me from re-inventing the wheel. Reading data from Excel is easy, and generating C# also. But I'm a lazy programmer.
Is there a code generation tool that accepts Excel files as input?
You can use T4 templates. I know this too common/abstract answer, but this is the way, I think, and I don't think you can find something already exist for that purpose. Visual Studio 2008 has native supports for them. In your T4 template you can read excel file and then use its data to create any code file you want.
I am guessing you will have to hand roll this solution. This does seem a little wonky, would be curious how you got in a situation where you are needing to do code gen based on Excel?!?
Having worked with this problem before, I suggest that you convert the excel file to a CSV.
Im my case, I transformed the CSV file into C# classes (which include property and method definitions).

Creating an Excel SpreadsheetML in code. (Without Excel!)

With Excel 2003 and higher it is possible to use the SpreadsheetML format to generate Excel spreadsheets with just an XML stylesheet and XML data file. I've used this in some project and works quite nice, even though it's not easy to do.
From the Microsoft Download site I've downloaded the XSD's that make up SpreadsheetML and in my ignorance, I've tried to convert them to C# classes. Unfortunately, xsd.exe isn't very happy about these schema files so I tend to be stuck.
I don't need an alternative solution to SpreadsheetML since it works fine for my needs. It's just that my code would be a bit easier to maintain for my team members if it's not written in a complex stylesheet. (It sucks to be the only XSLT expert in your company.)
All I want to know if someone has successfully created Excel SpreadsheetML files with .NET without the use of third-party code and without XSLT. And if you do, how did you solve this?
(Or maybe I just have to discover how to add namespaces to XML elements within XML.Linq...)
A while ago I used the XmlDocument and friends to create a SpreadsheetML document with formulae, formats and so on, so it is possible if a bit fiddly.
This MSDN page is what you need to get started with using the namespace in LINQ.
I have used this library and there is even a tool to generate the C# code that you need from an exsisting excel file.
http://www.carlosag.net/Tools/ExcelXmlWriter/
I had started on a similar problem a few weeks back, but due to some impending issues I had to put it at the back burner.
Back then I referred to this http://www.codeproject.com/KB/aspnet/ExportClassLibrary.aspx?fid=113399&df=90&mpp=25&sort=Position&tid=2609600
I really couldn't get started with it but plan to get back on it soon. I hope the link helps.
cheers

Categories