How to write tab-delimited text into excel sheet programmatically? - c#

I have an object of StringBuilder which stores a tab-delimited text and I want to import it to an Excel sheet. In my application it can be achieved in two ways. From a dialog one can choose either copying the text to the clipboard (and then he/she can open a new Excel sheet and simply paste in Excel) or saving it directly to an Excel workbook. First option works perfectly fine and I want to implement the behaviour for saving directly.
The only way that I found on the internet is storing each line of the text to the string array and then each index of array will be new row in Excel sheet, so I can write each row by iterating the array.
for (int i = 0; i < exportStrings.Count; i++)
{
Excel.Range currentRange = (Excel.Range)xlWsh.Cells[i + 1, 1];
currentRange.Value = exportStrings[i];
}
But then it causes a problem for the first option. Since the text is now stored as a string array, I cannot easily copy it to the clipboard. That is why I decided to keep my text stored as a stringbuilder. I also tried to write the text to the very first cell in the Excel sheet, (since it works when I paste the text to first cell in Excel sheet manually in the first option) but it doesn't recognise the new line charactes.
Excel.Range currentRange = (Excel.Range)xlWsh.Cells[1, 1];
currentRange.Value2 = exportStrings.ToString();
Any idea how it can be done using stringbuilder? or how can I make it to recognise new line character?

You can always extract a string from your string builder, and then split it into lines, if you need a string array:
StringBuilder sb = new StringBuilder();
//fill sb with whatever data you need
if (option1)
{
//use stringbuilder, copy to clipboared
}
if (option2)
{
//convert to string array
String allText = sb.ToString();
string[] textLines = allText.Split(Environment.NewLine);
//write to Excel...
}
Admittedly, this is not perfect in terms of efficiency: You are creating an intermediate string, and then you again allocate memory for the string array. But I am going to assume that you are dealing with a limited amount of data, and that the interaction with Excel takes more time anyway than a few object allocations.

Related

How to AppendAllText in excel file for multi-variables in multi-columns using c#?

The following code line allows me to append the variable w1 in excel file. but appending process happens in the first column. how to make it for multi-variables {w1, w2, w3} in multi-columns?
File.AppendAllText(Path.Combine(docPath, "Groupbox1.CSV"), w1);
First, you're actually working with .CSV files, not Excel. The CSV stands for Comma Separated Values, so each value in a line of text on these files are typically separated by a comma (or other delimiters such as semicolon). The Excel application lets you open .CSV files, but they are different from Excel files which have either .xls or .xslx extensions. Excel will display values separated by commas in their own columns.
Then, say you have multiple variables. Then you can simply create one string with commas in between values, and write it to the file.
var w1 = "Data1";
var w2 = "Data2";
var w3 = "Data3";
// This creates a string, according to above data, that looks like
// "Data1,Data2,Data3"
var lineToWrite = string.Format("{0},{1},{2}", w1, w2, w3);
File.AppendAllText(Path.Combine(docPath, "Groupbox1.CSV"), lineToWrite);
Now we write that line to the file, and each data item is separated by a comma. Now when you open this with Excel, each data item will be displayed in its own column.

Exporting CSV File to C# with a line breaks in cells

My application is extracting data from a CSV excel file and distribute each lines in an array of string, and then distributes the array of string to another array of string to form a cell, right? here's my problem, some of the lines that has been extracted and distribute to an array of string from the csv file is cut short, and does not collect the array of strings completely. and we noticed that that array of string when we looked it inside the excel has a cell that has a line break:
here is the C# code when extracting the csv file's lines to an array:
var lines = File.ReadAllLines(fileName);

C# trouble creating a table in console

I'm struggle to get data to display in a table within a console application. I believe it may be something to-do with the way I am getting the data to display.
I'm using this to read the content of text files:
string currentDir = Directory.GetCurrentDirectory();
string[] textFiles = Directory.GetFiles(currentDir, "*.txt");
string[] lines = new string[11];
for (int i = 0; i < textFiles.Length; i++)
{
lines[i] = File.ReadAllText(textFiles[i]);
}
Then I'm trying to display all the content of the text file into a table, each text file has 600 entries and they all go together to make a table.
Console.WriteLine("{0,10} \t {1,15}", lines[0], lines[1]);
Was my attempt of getting them to display in a table but only the last entry of lines[0] and first entry of lines[1] are being put on the same line in console... Anyone have any ideas?
You're reading the entire contents of a file in a simple string, so that's what you'll get in that string - the contents of the file, including any new lines, tabs, spaces and so on. If you want to manipulate individual bits of strings within those files, you'll need to split those strings according to some rules first.
The formatting alignment you're using doesn't do all that much by itself - see Alignment Component in https://msdn.microsoft.com/en-us/library/txafckwd.aspx

importing Data table to Excel text becomes numeric

Hi just want to ask I am trying to import Datatables to excel and one of the columns becomes exponent like 444201000100100 becomes 4442exp8? here's my script
protected void btnExportfromDt_Click(object sender, EventArgs e)
{
string strFilename = "FinanceforBulkupload.xls";
UploadDataTableToExcel(LoadData(), strFilename);
}
protected void UploadDataTableToExcel(DataTable dtEmp,string filename)
{
string attachment = "attachment; filename="+filename;
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/vnd.ms-excel";
string tab = string.Empty;
foreach (DataColumn dtcol in dtEmp.Columns)
{
Response.Write(tab + dtcol.ColumnName);
tab = "\t";
}
Response.Write("\n");
foreach (DataRow dr in dtEmp.Rows)
{
tab = "";
for (int j = 0; j < dtEmp.Columns.Count; j++)
{
Response.Write(tab + Convert.ToString(dr[j]));
tab = "\t";
}
Response.Write("\n");
}
Response.End();
}
Pre-append a single quote to the value before you store it. Instead of:
444201000100100
store:
'444201000100100
must write the cell formatted text but the method we're using to write the cell does not allow you to add formatting, you should try another method to write the Excel file
You can also format the destination column to be of type text (or whatever Excel format you'd prefer), to prevent it from doing this as well as removing leading zeros and other interpretations of the "General" cell formating rules.
As you're creating your output document on-the-fly, you'd need to set either specific columns or the entire worksheet to have this format rule. in VBA, this command is:
Columns("A:A").NumberFormat = "#" ' This is the Text format
In C#, you should be able to do something along the lines of:
Excel.Range rng = this.Application.get_Range("A:A");
rng.NumberFormat = "#";
However this would require a reference to the Excel object, and from your existing code it looks as though you're working without that for the moment.
Gary's Student's answer will work as well, but doing so will cause those cells to ignore any formatting that is applied later when it is opened in Excel. The user would be required to remove the leading ' manually to control the format as expected.
You have different options as others proposed.
Understand that you are generating a CSV and not an Excel file so you are limited.
Since a cell format is an excel property and cannot be specified in a CSV file, I would recommend creating an excel file (.xlsx) with EPPlus and write code to specify the column format as TravelinGuy suggested. http://epplus.codeplex.com/
If you want to stick to CSV then you are limited as to how the data will be displayed in Excel. Either you accept that Excel infers a number format from what it is reading or you put a leading quote...
Unfortunately, you have to manually perform minor encoding when sending data to Excel. This is true of the automation API as well as any text-based file formats like CSV or (as in your case) tab-separated files.
A robust solution must prepend a single ' character to a string in the following cases.
The string can be parsed as a number (Excel uses VariantChangeTypeEx, but since you're using C#, Double.TryParse will do the trick)
The string already already starts with a tick ('). (For example, if you have the string 'quoted', Excel will interpret it as quoted' unless you put another single tick at the front -- ''quoted'.)
The string starts with an = character. Even in the case of CSV files, Excel inteprets this to be a worksheet function. Putting a tick at the front prevents this (e.g. '=This is not a worksheet function).
If you still find this unacceptable, please know that you have no other option when you're writing text files manually like you're doing. If you object so strongly, you can try using the Open XML SDK or some other 3rd-party tool for building Excel workbooks. However, given what looks like a very simple use case, you'll almost certainly end up with a more complex solution that is slower and requires more code. I recommend just sucking it up and writing a simple routine that abides by the rules above.

.txt file to .xlsx file (600 rows and 40 columns)

I have a program that combines three text files and put them all into one and sorts them all out alphabetically. I was wondering how I could possibly put this onto an excel spreadsheet without downloading and using the excellibrary (if that's possible).
Heres my code that combines all three files if that helps.
private void button1_Click(object sender, EventArgs e) // merge files button
{
System.IO.StreamWriter output = new System.IO.StreamWriter("OUTPUT.txt");
String[] parts = new String[1000];
String[] parts2 = new String[1000];
parts = File.ReadAllLines(textBox1.Text); //gets filepath from top textbox
parts2 = File.ReadAllLines(textBox2.Text); //gets filepath from middle textbox
String[] head = File.ReadAllLines(headingFileBox.Text); //header file array
//merging the two files onto one list, there is no need to merge the header file because no math is being
//computed on it
var list = new List<String>();
list.AddRange(parts);
list.AddRange(parts2);
//foreach loop to write the header file into the output file
foreach (string h in head)
{
output.WriteLine(h);
}
//prints 3 blank lines for spaces
output.WriteLine();
output.WriteLine();
output.WriteLine();
String[] partsComb = list.ToArray(); // string array that takes in the list
Array.Sort(partsComb);
//foreach loop to combine files and sort them by 1st letter
foreach (string s in partsComb)
{
partsComb.Equals(s);
output.WriteLine(s);
}
output.Close();
}
Any help would be much appreciated.
You could look at creating it in a CSV format (Comma-separated values). Excel naturally opens it up and loads the data into the rows and cells.
Basic CSV looks like this:
"Bob","Smith","12/1/2012"
"Jane","Doe","5/10/2004"
Some things are optional like wrapping everything in quotes, but needed if your data may contain the delimiter.
If you're okay with a comma separated values (CSV) file, that's easy enough to generate with string manipulation and will load in Excel. If you need an excel specific format and are okay with XLSX, you can populate one with some XML manipulation and a ZIP library.
Fair warning, you will have to be careful about escaping commas and new lines if you choose a traditional CSV file. There are libraries that handle that as well.
You might want to try Excel package plus: http://EPPlus.codeplex.com
It's free, lightweight, and can create xlsx files.

Categories