I’m taking database records and trying to output them to an excel file using the following code:
Hi,
Thanks for your query.
Well, once you have imported data to the worksheet using Cells.ImportDataReader() method, you got to specify the text wrapping on for the cell(s)/ range of cells. You may simply create a Style object and set/ specify the wrapping text option on (you may also create the StyleFlag object and make the relevant option on), now apply the style to the cell/ range of cells/column/row etc. Please see the documents for your reference:
Line Breaks and Text Wrapping
Configuring Alignment Settings
Thank you.
There seems to be a different in the way putValue put data in the cell vs importdatareader.
Hi,
Hi,
e.g
Sample code:
OleDbConnection con = new OleDbConnection(“provider=microsoft.jet.oledb.4.0;data source=e:\test2\Northwind1.mdb”);
con.Open();
OleDbCommand cmd = new OleDbCommand(“Select EmployeeID,Title from Employees”, con);
OleDbDataReader dr = cmd.ExecuteReader();
dr.Read();
Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets.Add(“DataSheet”);
sheet.Cells.ImportDataReader(dr, true, 0, 0, true);
Worksheet _worksheet = workbook.Worksheets[“DataSheet”];
//Create a style object
Aspose.Cells.Style style = _worksheet.Cells[1, 1].GetStyle();
//Set wrapping text on
style.IsTextWrapped = true;
//Apply the style to the cell
_worksheet.Cells[1, 1].SetStyle(style); //it does not apply style to B2 cell, it still shows ‘\n’ char in the cell.
_worksheet.AutoFitRow(0);
Cell cell = sheet.Cells[“B14”];
cell.PutValue(“Vice President,\nSales”);
style = _worksheet.Cells[“B14”].GetStyle();
//Set wrapping text on
style.IsTextWrapped = true;
//Apply the style to the cell
_worksheet.Cells[“B14”].SetStyle(style); //It works fine if I used Cell.PutValue method.
_worksheet.AutoFitRow(13);
workbook.Save(“e:\test2\out1importfromdatareader1.xlsx”);
dr.Close();
I found that if I put in \r\n instead of just \n, the importdatareader function will break (so long as the istextwrapped is set.
Hi,
Here is my exact code. Notice I have hardcoded a \r\n as the fieldname for now for the B column:
Hi,
Hi,
I’m using SQL server as my database, and can’t figure out what ASCII string to insert that would represent the CTRL+ENTER.
Hi,
If I hard code ‘first’ + CHAR(13) + CHAR(10) + ‘second’ in my sql statement select I get it to wrap. However, the autofit columns doesn’t seem to be allowing the space. I’ll attach my excel.
Hi,
Ok, I shall continue with the method of inserting the\r\n in the database.