Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WrapText for column in Excel programmatically

I'm trying to set WrapText property to true with C#.

Range rng = sheet.get_Range("A:A", System.Type.Missing);
rng.EntireColumn.ColumnWidth = 50;
rng.EntireColumn.AutoFit();
rng.EntireRow.AutoFit();
rng.WrapText = true;

but it doesn't work without any exceptions. What's wrong? Thank you!

like image 876
Max Zhukov Avatar asked Apr 26 '14 23:04

Max Zhukov


People also ask

How can I wrap text in Excel?

In a worksheet, select the cells that you want to format. On the Home tab, in the Alignment group, click Wrap Text. (On Excel for desktop, you can also select the cell, and then press Alt + H + W.)


1 Answers

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Excel = Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
using System.IO;

//Initial Declarations//
Excel.Workbook destinationXlWorkBook;
Excel.Worksheet destinationXlWorkSheet;
Excel.Application destinationXlApp;
object misValue = System.Reflection.Missing.Value;

//Launch Excel App//
destinationXlApp = new Excel.Application();

//Load WorkBook in the opened Excel App//
destinationXlWorkBook = destinationXlApp.Workbooks.Add(misValue);

//Load worksheet-1 in the workbook//
destinationXlWorkSheet =
 (Excel.Worksheet)destinationXlWorkBook.Worksheets.get_Item(1);

//Set Text-Wrap for all rows true//
destinationXlWorkSheet.Rows.WrapText = true;

//Or, set it for specific rows//
destinationXlWorkSheet.Rows[3].WrapText = true;
destinationXlWorkSheet.Rows[5].WrapText = true;

//Edit individual cells//
 xlWorkSheet.Cells[1, 1] = "ID";                  
 xlWorkSheet.Cells[1, 2] = "Name";
 xlWorkSheet.Cells[2, 1] = "1";
 xlWorkSheet.Cells[2, 2] = "One";
 xlWorkSheet.Cells[3, 1] = "2";
 xlWorkSheet.Cells[3, 2] = "Two";


//Save and close the Excel//
destinationXlWorkBook.SaveAs("C:\\Users\\UtkarshSinha\\Documents\\SAP\\text.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);

//Start quitting and closing Excel//
destinationXlWorkBook.Close(true, misValue, misValue);
destinationXlApp.Quit();
Marshal.ReleaseComObject(destinationXlWorkSheet);
Marshal.ReleaseComObject(destinationXlWorkBook);
Marshal.ReleaseComObject(destinationXlApp);
like image 147
monkSinha Avatar answered Oct 03 '22 08:10

monkSinha