Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Tue Sep 28, 2021 3:18 am

This piece of code loops through all files in a particular location and converts all.csv files to pdf. It was working as desired with the trial version. Today, I applied the license for our full version, and the wrap text feature is no longer working for column "E". This is the only column that needs to wrap. I feel like I've exhausted the tutorials and forum posts. Does anyone have an idea why the wrap for this column is not working? Thanks.

NOTE: proprietary info replaced with "xxx" where applicable.

Code: Select all


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spire.Xls;
using System.IO;


namespace ConvertCSVToPDF
{
   
    class Program
    {

        static void Main(string[] args)
        {
            Spire.License.LicenseProvider.SetLicenseFileFullPath(xxx);
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

            try
            {
                //Set directory where files for conversion will be found.
                DirectoryInfo directory = new DirectoryInfo(xxx);


                //For loop, look through all .csv files in specified location.
                var files = directory.GetFiles("*.csv");

                foreach (var file in files)
                {
                    //Capture current filename
                    string filename = file.FullName.ToString();

                    //Define variable for workbook
                    Workbook workbook = new Workbook();

                    //Load the document from disk
                    workbook.LoadFromFile(filename, ",", 1, 1);

                    //Set the SheetFitToPage property as true
                    //workbook.ConverterSetting.SheetFitToPage = true;


                    //Get the first worksheet
                    Worksheet sheet = workbook.Worksheets[0];

                    foreach (Worksheet sheet2 in workbook.Worksheets)
                    {
                        //Change the page size
                        sheet2.PageSetup.PaperSize = PaperSizeType.PaperA3;
                    }

                    //Set left header
                    sheet.PageSetup.LeftHeader = "xxx";

                    //Set right header               
                    sheet.PageSetup.RightHeader = "xxx";

                    //Set center header.  Use fiscal quarter of run date.
                    int mth = DateTime.Now.Month;
                    if (mth <= 3)
                    {
                        sheet.PageSetup.CenterHeader = "Quarter 1 " + DateTime.Now.Year;
                    }
                    else if (mth > 3 && mth <= 6)
                    {
                        sheet.PageSetup.CenterHeader = "Quarter 2 " + DateTime.Now.Year;
                    }
                    else if (mth > 6 && mth <= 9)
                    {
                        sheet.PageSetup.CenterHeader = "Quarter 3 " + DateTime.Now.Year;
                    }
                    else if (mth > 9 && mth <= 12)
                    {
                        sheet.PageSetup.CenterHeader = "Quarter 4 " + DateTime.Now.Year;
                    }

                    sheet.ViewMode = ViewMode.Layout;

                    //Set center footer.
                    sheet.PageSetup.CenterFooter = "&P" + "/" + "&N";

                    //Get the cell range where you want to apply border style
                    CellRange cr = sheet.Range[sheet.FirstRow, sheet.FirstColumn, sheet.LastRow, sheet.LastColumn];

                    //Apply border style
                    cr.Borders.LineStyle = LineStyleType.Thin;
                    cr.Borders[BordersLineType.DiagonalDown].LineStyle = LineStyleType.None;
                    cr.Borders[BordersLineType.DiagonalUp].LineStyle = LineStyleType.None;
                    cr.Borders.Color = Color.Black;

                    //Set specific column widths where necessary
                    //to preserve formatting as defined by business owner.
                    sheet.SetColumnWidthInPixels(1, 80);
                    sheet.SetColumnWidthInPixels(2, 100);
                    sheet.SetColumnWidthInPixels(3, 100);
                    sheet.SetColumnWidthInPixels(4, 80);
                    sheet.SetColumnWidthInPixels(5, 200);
                    sheet.SetColumnWidthInPixels(6, 80);
                    sheet.SetColumnWidthInPixels(7, 75);
                    sheet.SetColumnWidthInPixels(8, 100);
                    sheet.SetColumnWidthInPixels(9, 75);
                    sheet.SetColumnWidthInPixels(10, 100);

                    //Wrap the excel text;                   
                    sheet.Range["E2:E1000"].Style.WrapText = true;

                    //Create a style
                    CellStyle style = workbook.Styles.Add("headerStyle");

                    //Apply border style
                    style.Borders.LineStyle = LineStyleType.Thin;
                    style.Borders[BordersLineType.DiagonalDown].LineStyle = LineStyleType.None;
                    style.Borders[BordersLineType.DiagonalUp].LineStyle = LineStyleType.None;
                    style.Borders.Color = Color.Black;

                    //Set the shading color
                    style.Color = Color.FromArgb(217, 217, 217);

                    //Set the font color
                    style.Font.Color = Color.Black;

                    //Set font size
                    style.Font.Size = 11;

                    //Set bold for the font
                    style.Font.IsBold = true;

                    //Set alignment
                    style.HorizontalAlignment = HorizontalAlignType.Center;
                    style.VerticalAlignment = VerticalAlignType.Center;

                    //Set the style for the specific range
                    workbook.Worksheets[0].Range["A1:J1"].CellStyleName = style.Name;

                    //Save to PDF document
                    string output = filename.Replace(".csv", ".pdf");
                    workbook.SaveToFile(output, FileFormat.PDF);

                    //Delete the CSV file
                    File.Delete(filename);
                }
            }
            catch (DirectoryNotFoundException dirNotFound)
            {
                Console.WriteLine(dirNotFound.Message);
            }

        }
    }
}


UnitedPhysicians
 
Posts: 3
Joined: Tue Aug 10, 2021 7:01 pm

Tue Sep 28, 2021 5:56 am

Hello Debbie,

Thanks for your inquiry.
I simulated a .csv file to test your code with the latest version(Spire.XLS Pack(Hotfix) Version:11.9.3), but didn't replicate your issue, the wrap text feature worked. Could you please share your .csv file to help me reproduce it?
Thanks for your assistance in advance.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Tue Sep 28, 2021 11:33 am

Thanks Amy. I've uploaded a sample attachment.
Attachments
wrap_test.zip
Test data
(428 Bytes) Downloaded 290 times

UnitedPhysicians
 
Posts: 3
Joined: Tue Aug 10, 2021 7:01 pm

Tue Sep 28, 2021 12:18 pm

I think I've discovered something. I was able to get it working as well. However, I was wondering if there is a limit when using a range for the wrap property? When I use a range from row 2 to row 1000, it seems to work fine. But when I apply my upper limit, 25,000, the wrap seems to not work for any rows.

UnitedPhysicians
 
Posts: 3
Joined: Tue Aug 10, 2021 7:01 pm

Wed Sep 29, 2021 5:55 am

Hello Debbie,

Thanks for your feedback.
In your code, you used the line sheet.Range["E2:E1000"].Style.WrapText = true to limit the cell range to row 1000. If you want more or all of the rows can be wrapped, please set the corresponding cell range, for example,
Code: Select all
int rowCount = sheet.LastRow;
 sheet.Range["E2:E"+rowCount].Style.WrapText = true;


Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Thu Sep 30, 2021 9:40 am

Hello Debbie,

Has this issue been solved? Any feedback will be appreciated.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.XLS