I need to export Date, Title and Description to excel file, right now i am facing two issue with the export of excel file.
one special characters such as '," an other characters turn into ‘ & etc....
All these issue are with the Description column, which stored text in HTML format. Below is the example of text in various formats
Actual Text
The ‘ Golf Season Opening’ marked the official opening of the at Golf Club, Season to start on March 10, 2018.
Text Stored in Database MS SQL SERVER
The ‘Golf Season Opening ‘ marked the official opening of the at Golf Club& Season to start on March 10& 2018.
Text exported to Excel
The ‘Golf Season Opening ‘ marked the official opening of the at Golf Club& Season to start on March 10& 2018.
I am using below code to create excel file but i am facing above issue.
How can i store text without being decoding text is excel should be store in text format & all special characters show properly without any issue
var wb = new XLWorkbook();
var ws = wb.Worksheets.Add("Calendar");
DataTable dt = ds.Tables[0];
var rowIndex = 2; // 1 = header row
foreach (DataRow row in dt.Rows)
{
ws.Cell("A" + rowIndex).Value = row["Year"];
ws.Cell("B" + rowIndex).Value = row["Title"];
string noHTML = Regex.Replace(row["Description"].ToString(), #"<[^>]+>| ", "").Trim();
string noHTMLNormalised = Regex.Replace(noHTML, #"\s{2,}", " ");
ws.Cell("C" + rowIndex).Value = noHTMLNormalised;
rowIndex++;
}
//// From worksheet
var rngTable = ws.Range("A1:C" + rowIndex);
var rngHeader = ws.Range("A1:C1");
var rngYear = ws.Range("A2:A" + rowIndex);
//var rngDate = ws.Range("B2:B" + rowIndex);
var rngTitle = ws.Range("B2:D" + rowIndex);
var rngDesc = ws.Range("C2:C" + rowIndex);
rngHeader.Style.Fill.SetBackgroundColor(XLColor.CoolGrey);
rngHeader.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
rngHeader.Style.Font.Bold = true;
rngHeader.Style.Font.FontColor = XLColor.White;
// rngYear.Style.Fill.SetBackgroundColor(XLColor.CoolGrey);
rngYear.Style.Font.Bold = true;
rngYear.Style.Font.FontColor = XLColor.Black;
rngYear.Style.Alignment.Indent = 1;
//rngDate.Style.DateFormat.Format = "MM/DD/YYYY";
//rngDate.Style.Alignment.Indent = 10;
rngDesc.Style.Alignment.SetWrapText();
ws.RangeUsed().Style.Border.OutsideBorder = XLBorderStyleValues.Thick;
var col3 = ws.Column("C");
//col3.Style.Fill.BackgroundColor = XLColor.Red;
col3.Width = 100;
ws.Columns().AdjustToContents();
string fileName;
fileName = "Golf_Calendat.xlsx";
wb.SaveAs(HttpContext.Current.Server.MapPath("../excel/" + fileName));
Any help to fixed the above issue and also if we we can wrap the text in description column and if row can take the auto height based on the wrapped text.
Just to mention i am using using Excel = Microsoft.Office.Interop.Excel; for excel export
you can replace it in a string
str.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
Solved Both issue with following code
First by using HTML HttpUtility.HtmlDecode
string htmlDec = HttpUtility.HtmlDecode(row["Description"].ToString());
and text wrap issue with ws.Column(2).AdjustToContents(5, 7);
string htmlEnc = HttpUtility.HtmlEncode(row["Description"].ToString());
string htmlDec = HttpUtility.HtmlDecode(row["Description"].ToString());
string noHTML = Regex.Replace(htmlDec, #"<[^>]+>| ", "").Trim();
string noHTMLNormalised = Regex.Replace(noHTML, #"\s{2,}", " ");
ws.Cell("C" + rowIndex).Value = noHTMLNormalised;
Related
This block of code throws an error called file name is invalid.
I want to create a folder named as "test" inside this there will be another folder named as today's date "date" , i want to keep the the word document inside this date folder, please help.
public string File_path;
public string docfile_path;
public string filename;
private void button1_Click(object sender, EventArgs e)
{
string time = DateTime.Now.ToString("HH.mm.ss");
string date = DateTime.Today.ToShortDateString();
docfile_path = File_path+ "test" + date;
Directory.CreateDirectory(docfile_path);
filename = docfile_path + "worddoc"+"-" +".docx";
Word.Application app = new Word.Application();
Word.Document doc = new Word.Document();
try
{
doc = app.Documents.Open(filename);
}
catch
{
}
Word.Paragraph oPara1;
oPara1 = doc.Content.Paragraphs.Add();
oPara1.Range.Text = "Test Result";
oPara1.Range.Font.Bold = 1;
oPara1.Format.SpaceAfter = 24;
oPara1.Range.InsertParagraphAfter();
oPara1.Range.InsertParagraphAfter();
Word.Paragraph oPara2;
oPara2 = doc.Content.Paragraphs.Add();
oPara2.Range.Text = "Test Name";
oPara2.Range.Font.Bold = 1;
oPara2.Format.SpaceAfter = 24;
oPara2.Range.InsertParagraphAfter();
doc.SaveAs2(filename);
doc.Close();
doc = null;
app.Quit();
app = null;
}
Surprisingly enough, this code compile and run, but the outcome is not what you probably wanted.
A couple of things is wrong in this code:
1.you cant add strings like that to create a path, a path should be created with the '/' symbol between directories.
this is a legal path:
string path = #"C:\Users\username\Desktop\Games";
this is not :
string path = #"C:UsersusernameDesktopGames";
you can fix it by using the Path.Combine function as follow:
docfile_path = Path.Combine(File_path , "test" , date);
be sure to this for all path strings (including File_path that is value is not shown in the code above).
2.you should use
Document doc = app.Documents.Add();
to create a new Word document and not
Document doc = new Document();
3.you should use a different format for string date, DateTime.ToShortDateString() is dividing the date with the '/' symbol which will create new folders.
try using:
string date = DateTime.Today.ToString("dd.MM.yyyy");
4.I don't see any reason for the line
doc = app.Documents.Open(filename);
You are trying to open the the file that you intent to create?
here is the code i used:
string File_path = #"C:\Users\yakir\Desktop";
string docfile_path;
string filename;
string time = DateTime.Now.ToString("HH.mm.ss");
string date = DateTime.Today.ToString("dd.MM.yyyy");
docfile_path = Path.Combine(File_path , "test" , date);
Directory.CreateDirectory(docfile_path);
filename = Path.Combine(docfile_path, "worddoc" + "-" + ".docx");
Application app = new Application();
Document doc = app.Documents.Add();
Paragraph oPara1;
oPara1 = doc.Content.Paragraphs.Add();
oPara1.Range.Text = "Test Result";
oPara1.Range.Font.Bold = 1;
oPara1.Format.SpaceAfter = 24;
oPara1.Range.InsertParagraphAfter();
oPara1.Range.InsertParagraphAfter();
Paragraph oPara2;
oPara2 = doc.Content.Paragraphs.Add();
oPara2.Range.Text = "Test Name";
oPara2.Range.Font.Bold = 1;
oPara2.Format.SpaceAfter = 24;
oPara2.Range.InsertParagraphAfter();
doc.SaveAs2(filename);
doc.Close();
doc = null;
app.Quit();
app = null;
}
I have written some code that reads every row in an excel file (for two specific columns) which I will be using later to execute an update SQL Query for each of the rows with a value.
I have displayed these values in a listbox, and I am getting far more nulls than expected when comparing with the stock codes in the excel file.
I have tried changing the formatting of the excel file, but this did not make any difference. There are rows where there definitely are stock codes at that position, but when the program does the cell comparison the program identifies them as nulls when they actually have values.
Does anyone know what the problem is with my code?
private void btnStockCodes_Click(object sender, RoutedEventArgs e)
{
string file = #"\\amn-fs-01\users$\Shanel\Desktop\Stock Codes.xlsx";
Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
Workbook ExcelWorkbook = ExcelApp.Workbooks.Open(file);
Worksheet ews = ExcelApp.ActiveWorkbook.Sheets[1];
Microsoft.Office.Interop.Excel.Range usedRange = ews.UsedRange;
int TotalCounter = 0;
string StockCode = "";
string ReserveID = "";
int nullcounter = 0;
int foundcounter = 0;
foreach (Microsoft.Office.Interop.Excel.Range row in usedRange.Rows)
{
StockCode = "";
ReserveID = "";
TotalCounter = TotalCounter + 1;
if (row.Cells[TotalCounter,7].Value == null)
{
Listbox1.Items.Add(TotalCounter + " null");
nullcounter = nullcounter + 1;
}
else
{
StockCode = row.Cells[TotalCounter,7].Value.ToString();
ReserveID = row.Cells[TotalCounter, 3].Value.ToString();
Listbox1.Items.Add(TotalCounter + " " + StockCode + " " + ReserveID);
foundcounter = foundcounter + 1;
}
}
txtTotal1.Text = foundcounter.ToString() + " Found";
txtTotal2.Text = nullcounter.ToString() + " Null Values";
txtTotal3.Text = TotalCounter.ToString() + " Total Records";
}
I would not trust that Worksheet.UsedRange always works correctly, sometimes it contains more cells than it should, or less. My suggestion is to read all rows in worksheet, while you have any values. Once there are no more values, just stop reading it.
And if you have too many rows, you can read all values at the same time into an array, like here and work with the array.
Thanks for your contributions, I have resolved the error!
It occurs in the row.Cells[TotalCounter,7].Value.ToString()
It should have been row.Cells[7].Value.ToString()
There was no need for me to specify a row index as that's taken care of in the Foreach loop. I will look into alternative ways of writing the code as Worksheet.UsedRange might not work in all cases as Alex suggested.
I'm starting to learn c # and Windows form. I create an application that transforms a resx (XML) file into an Excel.
All my code works, my Excel file is created and I can convert it to a resx file.
But, when I open my Excel file, spaces before and after my data has been added like this : Excel cell example. And when I convert it to resx file, it does
Resx file example
Here is my resx => excel code :
//I use a application WindowsForm so any 'LBL' / 'TXT make reference to label or textBox I use them to set file or folder path
private void writeExcel()
{
Dictionary<string, string> dataSave = new Dictionary<string, string>();
var path = LBL_DocumentPath.Text;
XDocument doc = XDocument.Load(path);
IEnumerable<XNode> nodes = doc.Descendants("data");
foreach (XElement node in nodes)
{
string name = node.Attribute("name").Value;
string value = node.Value;
dataSave.Add(name, value);
}
CreateExcel(dataSave);
}
private void CreateExcel(Dictionary<string, string> dico)
{
int i = 1;
FileInfo newFile = new FileInfo(LBL_FolderPath.Text + "/" + TXT_FileName.Text + ".xlsx");
using (ExcelPackage package = new ExcelPackage(newFile))
{
try
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Inventry");
worksheet.Cells[1, 1].Value = "Name";
worksheet.Cells[1, 2].Value = "value";
worksheet.Cells[1, 3].Value = "translation";
foreach (KeyValuePair<string, string> data in dico)
{
string testMessage = String.Format("{0}", data.Value);
string delSpace = testMessage;
Regex regex = new Regex(#"(\s){2,}");
testMessage = regex.Replace(delSpace, "&");
i++;
worksheet.Cells[i, 1].Value = String.Format("{0}", data.Key);
worksheet.Cells[i, 2].Value = String.Format("{0}", testMessage);
worksheet.Cells.AutoFitColumns();
}
package.Save();
MessageBox.Show("File created ! " + LBL_FolderPath.Text + "\\" + TXT_FileName.Text);
}
catch (Exception)
{
MessageBox.Show("File already exist, checks : " + LBL_DocumentPath.Text + "\\" + TXT_FileName.Text);
}
}
}
If you want all my code, I can give you a dropbox link.
Thanks in advance for any help you can give me.
Math.
Ps: My apologies, my English is not very good. I hope you will understand me correctly
Ok a friend give me solution.
It's my regex which does not work so I replace
string testMessage = String.Format("{0}", data.Value);
string delSpace = testMessage;
Regex regex = new Regex(#"(\s){2,}");
testMessage = regex.Replace(delSpace, "&");
by
string testMessage = String.Format("{0}", data.Value);
testMessage = testMessage.Replace("\n",string.Empty);
testMessage = testMessage.Replace("\r", string.Empty);
testMessage = testMessage.Replace(" ", string.Empty);
I want to add following text into MS-Word footer using MS-Interop Word DLL.
Required Footer Text:
"Page 1 of 10 and date = {Current Date}" something like this.
I have added below code which add page no. and current date but its not allowing me add any custom text like "Page 1 of 10".
Here is my code
foreach (Microsoft.Office.Interop.Word.Section wordSection in document.Sections)
{
Microsoft.Office.Interop.Word.Range footerRange = wordSection.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
footerRange.Collapse(Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd);
footerRange.Fields.Add(footerRange, Microsoft.Office.Interop.Word.WdFieldType.wdFieldDate,"Date = ");
footerRange.Fields.UpdateSource();
footerRange.Fields.Add(footerRange, Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage, "Page No = ");
footerRange.Fields.UpdateSource();
footerRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
}
An idea how to add such functionality?
Here is the solution which I have found.
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
foreach (Microsoft.Office.Interop.Word.Section wordSection in document.Sections)
{
Microsoft.Office.Interop.Word.Range footerRange = wordSection.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
footerRange.Collapse(Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd);
footerRange.Fields.Add(footerRange, Microsoft.Office.Interop.Word.WdFieldType.wdFieldNumPages);
Microsoft.Office.Interop.Word.Paragraph p4 = footerRange.Paragraphs.Add();
p4.Range.Text = " of ";
footerRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
footerRange.Fields.Add(footerRange, Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage);
Microsoft.Office.Interop.Word.Paragraph p1 = footerRange.Paragraphs.Add();
p1.Range.Text = "Page: ";
footerRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
Microsoft.Office.Interop.Word.Paragraph p3 = footerRange.Paragraphs.Add();
p3.Range.Text = " " + Environment.NewLine;
footerRange.Fields.Add(footerRange, Microsoft.Office.Interop.Word.WdFieldType.wdFieldDate);
Microsoft.Office.Interop.Word.Paragraph p2 = footerRange.Paragraphs.Add();
p2.Range.Text = "Print date: ";
footerRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
}
string Code = "";
if (fileUp.HasFile)
{
string Path = fileUp.PostedFile.FileName;
// initialize the Excel Application class
ApplicationClass app = new ApplicationClass();
// create the workbook object by opening the excel file.
Workbook workBook = app.Workbooks.Open(Path, 0, true, 5, "", "", true,
XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
// Get The Active Worksheet Using Sheet Name Or Active Sheet
Worksheet workSheet = (Worksheet)workBook.ActiveSheet;
int index = 0;
// This row,column index should be changed as per your need.
// that is which cell in the excel you are interesting to read.
object rowIndex = 2;
object colIndex1 = 1;
object colIndex2 = 2;
object colIndex3 = 3;
object colIndex4 = 4;
object colIndex5 = 5;
object colIndex6 = 6;
object colIndex7 = 7;
try
{
while (((Range)workSheet.Cells[rowIndex, colIndex1]).Value2 != null)
{
rowIndex = 2 + index;
//string QuestionCode = (index + 1).ToString();
string QuestionCode = ((Range)workSheet.Cells[rowIndex, colIndex1]).Value2.ToString();
string QuestionText = ((Range)workSheet.Cells[rowIndex, colIndex2]).Value2.ToString();
string CorrectAnswer = ((Range)workSheet.Cells[rowIndex, colIndex3]).Value2.ToString();
string ChoiceA = ((Range)workSheet.Cells[rowIndex, colIndex4]).Value2.ToString();
string ChoiceB = ((Range)workSheet.Cells[rowIndex, colIndex5]).Value2.ToString();
string ChoiceC = ((Range)workSheet.Cells[rowIndex, colIndex6]).Value2.ToString();
string ChoiceD = ((Range)workSheet.Cells[rowIndex, colIndex7]).Value2.ToString();
// string ChoiceE = ((Excel.Range)workSheet.Cells[rowIndex, colIndex7]).Value2.ToString();
newQuestionElement = new XElement("Question");
XElement optionElement = new XElement(QuestionElement.Option);
questionType = ddlQusType.SelectedValue.ToByte();
if (!string.IsNullOrEmpty(QuestionText))
newQuestionElement.Add(new XElement(QuestionElement.QuestionText, QuestionText));
else
{
//lblMessage.Text = "Missing question in Qus No.: " + i;
break;
}
newQuestionElement.Add(new XElement(QuestionElement.QuestionType, questionType));
//newQuestionElement.Add(new XElement(QuestionElement.Randomize, chbRandomizeChoice.Checked));
newQuestionElement.Add(new XElement(QuestionElement.Answer, CorrectAnswer));
if (ChoiceA.Trim() != string.Empty)
optionElement.Add(new XElement("A", ChoiceA));
if (ChoiceB.Trim() != string.Empty)
optionElement.Add(new XElement("B", ChoiceB));
if (ChoiceC.Trim() != string.Empty)
optionElement.Add(new XElement("C", ChoiceC));
if (ChoiceD.Trim() != string.Empty)
optionElement.Add(new XElement("D", ChoiceD));
newQuestionElement.Add(optionElement);
index++;
saveData(QuestionCode.ToString());
I am using this code to retrieve the data from .xlsx file.
But if the file has any special characters in it, it is showing it as different, like so
The set S = {1,2,33……….12} is to be partitioned into three sets
A,B,C of equal size. Thus, `A U B U C = S,`
The set S = {1,2,33……….12} is to be partitioned into three sets
A,B,C of equal size. Thus, `A È B È C = S,`
Looks like an encoding issue.
I use to have this issue after reading Excel into a data table and then serializing the data table to a file.
Every time I would read the data back in from the serialized file, some symbols would be replaced with funny A's and E's.
I discovered the problem was with the encoding I was using. I then started to store excel data using Unicode encoding and have never encounter another symbol problem with Excel data again.
I hope this helps...