I would like to use EPPlus to create an Excel file but I have a problem: my input data is in a tab-delimited format:
Name Code Grade
------------------------
N1 C22 17.6
N2 C09 18.9
N3 C18 20
How can I add this type (tab format) of data using EPPlus package?
EPPlus is a library to read and write xlsx files only, so you cannot directly parse a file in tabular format.
You need to either write a reader for your format or, even easier, use a CSV reader that supports custom delimiters, and set the delimiter to \t. You can use this reader to read each cell of your data and feed it to EPPlus to re-create the datasheet.
I would simply read the text file in the standard old C# way, load each cell into a cell in EPPlus, and then save it. You'd just have to write a few loops and some formatting code.
Epplus supports importing tab delimited text. You can see it sample9.cs (https://github.com/JanKallman/EPPlus/blob/master/SampleApp/Sample9.cs)
If you are just looking for code,
//Create the format object to describe the text file
var format = new ExcelTextFormat();
format.Delimiter='\t'; //Tab
//Now read the file into the sheet.
Console.WriteLine("Load the text file...");
var csvDir = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "csv");
var range = sheet.Cells["A1"].LoadFromText(Utils.GetFileInfo(csvDir, "Sample9-2.txt", false), format);
Direct link to the line # https://github.com/JanKallman/EPPlus/blob/master/SampleApp/Sample9.cs#L138
Related
Im exporting some data to a .csv file.
.csv file:
Hotel Name;Street;Postal Code;City;Latitude;Longitude
Hotel X;Street 1;00000;City X;15.000000;15.000000
But if i open it in Excel, Excel will Format the Latitude and Longitude automaticly so it cannot be used for copy & paste.
Ignore the 0 at postal Code, i must get rid of the 1.000 at Latitude and Longitude
How can i prevent Excel from doing this?
It should be done in the .csv file and not in Excel.
The Export Code:
foreach(...)
{
StringBuilder_Export.Append(DataRow_Temp[i] + ";");
}
StreamWriter StreamWriter_Export = new StreamWriter(
SaveFileDialog_Geo_Export.FileName,
true,
Encoding.Default
);
StreamWriter_Export.WriteLine(StringBuilder_Export.ToString());
EDIT: Im searching primarly for a solution of my Latitude and Longitude Problem.
Possibly a duplicate of Stop Excel from automatically converting certain text values to dates but if you want to generate an Excel file that looks exactly as you want when opened, do not use csv. Excel will always attempt to guess datatypes of csv columns by looking at the first (15 i think?) rows..
I use EPPlus to generate xlsx files from my apps, but there are many libraries you could use
Change its extension to txt and use the text import wizard. Then use that to tell excel how the columns should be treated (text, currency, date, etc). The text import wizard will start automatically when you open a .txt file
ensure the file is in UTF-8 format when you save it as a .csv
Then open excel, browse to select the file you want and it will automatically run the prompter.
Any other format than utf-8 and excel will try and auto convert.
For a project I'm working on, we need to copy data from Excel sheets into new tables within a Word document and have a strategy that works... in most cases.
First, we do
string file = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() +
".mht");
object sheetObj = sheetName;
object trueObj = true;
Excel.PublishObject obj = workbook.PublishObjects.Add(Excel.XlSourceType.xlSourceSheet, file,
sheetObj);
obj.Publish(trueObj);
then
Document tempDocument = wordApp.Documents.Open(file);
and read templDocument into the targe Word doc.
...
In a couple of cases, we're seeing problems. (The problems are illustrated in the example files at http://thinkscience.us/office/examples.zip)
1) the big text files show text truncation between Excel and the exported .mht
2) the 'nutritional' files show the addition of several lines of white space between the Excel data and the .mht.
I've tried several variations on the parameters to PublishObjects.Add (using a range rather than an entire sheet). The add method includes an optional XlHtmlType parameter that only works with value XlHtmlType.xlHtmlStatic.
Has anyone used PublishObjects.Add or another strategy to transfer sheets from Excel to Word, preserving as much formatting as possible and not interfering with the system clipboard?
I found an article which might meet your needs, but it's not using Office automation, it's using a library called Spire.Office. Check: How to Maintain Formatting of Cells when Copying Cells from Excel to Word.
I have an application, on which i export data from a datagrid to a csv file. I do this with the following steps:
Create a file:
var myFile = File.Create("test.csv");
myfile.Close();
write the data to a string builder(data)
write the data to the created file.
File.WriteAllText(filepath, data);
This works fine. The resulting csv file is opened in excel. I have a column of numbers which may have preceeding zeros, when those data is exported to csv file the preceeding 0's are lost. Is it possible to format the column as text column so the zeros are not lost.
View your file in notepad. The leading zeros are there, intact :-)
You need to tell Excel which format to use when you open the file. Change the file to .txt and use File -> Open in Excel and you should be presented with an import wizard. There you can explicitly tell Excel to treat your column as "text" which will prevent it from stripping leading zeroes.
More info here: http://www.howtogeek.com/howto/microsoft-office/how-to-import-a-csv-file-containing-a-column-with-a-leading-0-into-excel/
I am wring a csv file with some program.
I am facing a issue when I open it in the excel file and for some column if it exceeds the value more than 256 it automatically truncate it.
I learnt that by default the excel has number format TO 'GENERAL' if I could set it to 'TEXT' then it wouldn't truncate any part.
So is there any programmatic way which can set the number format from GENERAL to TEXT.
csv is a plain text format. You can't embed meta-data such as column handling into that file format.
If you don't want excel to truncate the column, then you'll need to truncate it yourself when you write out the csv file:
var csvFileBuffer = new StringBuilder();
var columns = new List<string>();
csvFileBuffer.AppendLine(
string.Join(
",",
columns.Select(s =>
//truncate column header (change logic as appropriate)
s.Substring(0, 255))));
That said, there may be an option you can set in excel which will change the default column type for a csv file. However, that would be a question for SuperUser.com
The other option is to write out to a native excel file format, such as xlsx. There are a number of tools you can use to this, such as the Open Xml Sdk.
I'm creating a program in a symbol ppt2800 PocketPC and we're gonna use it for doing a list of stuff in a store. The barcode reader in the symbol reader will be checked against a list in the PocketPC and if they match it will note it in a list. I'm gonna export a list from our checkout system and I can export it to the following formats:
pdf
csv
crystal report rpt
html 3.2 or 4.0
xls
word rtf
postformat rec
txt
rtf
ttx
xml.
My question is what format is the easiest one to convert to an array in C#? I need it to work on compact framework 1.1
For a simple list, just text, ideally just a record per line. Anything like PDF, Crystal or Word will need lots of processing - not suitable for pocket PC. For a simple txt file, that is just:
string[] entries = File.ReadAllLines(path);
If that isn't on CF, then just using a StreamReader instead:
List<string> list = new List<string>();
using(StreamReader reader = new StreamReader(path)) {
string line;
while((line = reader.ReadLine()) != null) {
list.Add(line);
}
}
string[] entries = list.ToArray();
(or ArrayList / CopyTo if you don't have access to generics!)
If you care about only easiest read into array, I would say, that CSV or TXT file with some predefined by you delimeter could be easiest one.
Just read the text into the string and make fileString.Split(delimeter)
XML can be the easiest if you can define the structure and deserialize it directly. If not then it depends on the data, but most probably csv is the simpliest choice IMHO.
Given your simple requirements I suggest to use txt, csv or xml.
These formats could be easily read with internal NET classes.
(The System.IO namespace will give plenty of options See this example)
The other formats will require complex specific converters.
I think XML or CSV file are the most easiest to parse.
You can parse XML with the basics class of the .NET framework, you can parse CSV with CsvHelper.
you should look into xml , and bin xmlserialization , and binary one