我尝试使用C#将WrapText属性设置为true。
Range rng = sheet.get_Range("A:A", System.Type.Missing);
rng.EntireColumn.ColumnWidth = 50;
rng.EntireColumn.AutoFit();
rng.EntireRow.AutoFit();
rng.WrapText = true;
但没有任何例外情况它不起作用。怎么了?谢谢!
答案 0 :(得分:2)
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);
答案 1 :(得分:1)
对于我的整个解决方案,我正在此处复制很多代码,TextWrap
是其中之一。我认为某天可能会派上用场。
主要计划:
public int counter;
public void ExportPartsToExcelButton(object sender, RoutedEventArgs e)
{
Type officeType = Type.GetTypeFromProgID("Excel.Application");
if (officeType != null)
{
if (CurrentID != 0)
{
counter = 1;
GeneratePartsXLSX();
}
else
{
MessageBoxEx.Show(this, "No address selected!", "Warning!", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
else
{
MessageBoxEx.Show(this, "Install Microsoft Excel!", "Warning!", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
public void GeneratePartsXLSX()
{
Mouse.OverrideCursor = Cursors.Wait;
string filename = GlobalStrings.building_house_address;
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\";
filename = filename.Replace("/", "+");
if (!File.Exists(path + filename + ".xlsx"))
{
DataGridParts.ExportPartsToExcel(path + filename + ".xlsx"); //You have to include the ".xlsx" extension, otherwise the Office interop detects a possible dot (.) in the file name as an extension. Example: road.block instead of road.block.xlsx
MessageBoxEx.Show(this, "Saved to Desktop");
}
else
{
if (!File.Exists(path + "\\" + filename + " (" + (counter) + ")" + ".xlsx"))
{
DataGridParts.ExportPartsToExcel(path + filename + " (" + (counter) + ")" + ".xlsx");
MessageBoxEx.Show(this, "Saved to Desktop");
}
else
{
counter++;
GeneratePartsXLSX();
}
}
Mouse.OverrideCursor = null;
}
CLASS:
using Microsoft.Office.Interop.Excel;
using System;
using System.Runtime.InteropServices;
namespace SAVETOEXCELPROGRAM
{
public static class ExportToExcel
{
public static void ExportPartsToExcel(this System.Data.DataTable DataTable, string ExcelFilePath = null)
{
int ColumnsCount;
int RowShift = 7;
ColumnsCount = DataTable.Columns.Count;
// load excel, and create a new workbook
Application Excel = new Application();
Excel.Workbooks.Add();
// single worksheet
_Worksheet Worksheet = Excel.ActiveSheet;
Excel.Sheets[1].Name = "WHATEVER";
Worksheet.Columns.NumberFormat = "@"; //Force the "Text" format, so that 1/2 won't get converted to 1st of February for example
Worksheet.Columns.HorizontalAlignment = XlHAlign.xlHAlignLeft; //Text aligment
Worksheet.Columns.VerticalAlignment = XlHAlign.xlHAlignCenter; //Text aligment
object[,] Title = new object[5, 1]; //Array range starts at [1,1], the content index starts at [0,0]
if (GlobalStrings.building_alterantive_addresses.Length == 0)
{
if (GlobalStrings.building_postcode.Length != 0)
{
Title[0, 0] = "DATE: " + DateTime.Now.ToString("dd.MM.yyyy - HH:mm");
Title[2, 0] = "ADDRESS: " + GlobalStrings.building_house_street + " " + GlobalStrings.building_house_number + GlobalStrings.building_house_id + ", " + GlobalStrings.building_postcode + " " + GlobalStrings.building_area;
Title[3, 0] = "C.C.: " + GlobalStrings.building_cadastral_community + ", BUILDING NO.: " + GlobalStrings.building_building_number + ", PLOT NO.: " + GlobalStrings.building_plot_number;
}
else
{
Title[0, 0] = "DATE: " + DateTime.Now.ToString("dd.MM.yyyy - HH:mm");
Title[2, 0] = "ADDRESS: " + GlobalStrings.building_house_street + " " + GlobalStrings.building_house_number + GlobalStrings.building_house_id;
Title[3, 0] = "C.C.: " + GlobalStrings.building_cadastral_community + ", BUILDING NO.: " + GlobalStrings.building_building_number + ", " + GlobalStrings.building_plot_number;
}
}
else
{
if (GlobalStrings.building_postcode.Length != 0)
{
Title[0, 0] = "DATE: " + DateTime.Now.ToString("dd.MM.yyyy - HH:mm");
Title[2, 0] = "ADDRESS: " + GlobalStrings.building_house_street + " " + GlobalStrings.building_house_number + GlobalStrings.building_house_id + ", " + GlobalStrings.building_postcode + " " + GlobalStrings.building_area;
Title[3, 0] = "C.C.: " + GlobalStrings.building_cadastral_community + ", BUILDING NO.: " + GlobalStrings.building_building_number + ", PLOT NO.: " + GlobalStrings.building_plot_number;
Title[4, 0] = "LOOK ALSO: " + GlobalStrings.building_alterantive_addresses;
}
else
{
Title[0, 0] = "DATE: " + DateTime.Now.ToString("dd.MM.yyyy - HH:mm");
Title[2, 0] = "ADDRESS: " + GlobalStrings.building_house_street + " " + GlobalStrings.building_house_number + GlobalStrings.building_house_id;
Title[3, 0] = "C.C.: " + GlobalStrings.building_cadastral_community + ", BUILDING NO.: " + GlobalStrings.building_building_number + ", PLOT NO.: " + GlobalStrings.building_plot_number;
Title[4, 0] = "LOOK ALSO: " + GlobalStrings.building_alterantive_addresses;
}
}
Range TitleRange = Worksheet.get_Range((Range)(Worksheet.Cells[5, 1]), (Range)(Worksheet.Cells[1, 1]));
TitleRange.Value = Title;
TitleRange.Font.Bold = true;
TitleRange.Font.Size = 10;
object[] Header = new object[14]; //Number of Headers
Header[0] = "PART";
Header[1] = "SHARE";
Header[2] = "CRP";
Header[3] = "+/-";
Header[4] = "OWNER";
Header[5] = "ADDRESS";
Header[6] = "POSTCODE";
Header[7] = "AREA";
Header[8] = "COUNTRY";
Header[9] = "SOCIAL";
Header[10] = "TYPE";
Header[11] = "DESCRIPTION";
Header[12] = "COMMENT";
Header[13] = "CODE";
int MaxCol = Header.Length;
Range HeaderRange = Worksheet.get_Range((Range)(Worksheet.Cells[RowShift, 2]), (Range)(Worksheet.Cells[RowShift, MaxCol + 1])); //MaxCol+1, because we have to shift the Array position by 1, as the first column is set to "ColumnWidth = 0.1"
HeaderRange.Value = Header;
HeaderRange.Font.Bold = true;
HeaderRange.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGray);
HeaderRange.Borders.LineStyle = XlLineStyle.xlContinuous;
// DataCells
int RowsCount = DataTable.Rows.Count;
object[,] Cells = new object[RowsCount, ColumnsCount]; //Array range starts at [1,1], the content index starts at [0,0]
for (int j = 0; j < RowsCount; j++)
{
for (int i = 3; i <= ColumnsCount - 2; i++) //With this you control the data range, starts with 0, you can exclude data with an " if (i != 0 && i != 1 && ... && i= 5)
{
Cells[j, i - 3] = DataTable.Rows[j][i]; //with i you control the horizontal placement of the data in the worksheet
}
}
Range CellRange = Worksheet.get_Range((Range)(Worksheet.Cells[RowShift + 1, 2]), (Range)(Worksheet.Cells[RowShift + RowsCount, MaxCol + 1])); //MaxCol+1, because we have to shift the Array position by 1, as the first column is set to "ColumnWidth = 0.1"
CellRange.Value = Cells;
CellRange.Borders.LineStyle = XlLineStyle.xlContinuous;
Worksheet.Columns[1].ColumnWidth = 0.1; //If this was set on AutoFit the Column 1 would get extended to the Title Width, because they share the column.
Worksheet.Columns[2].ColumnWidth = 3;
Worksheet.Columns[3].ColumnWidth = 4;
Worksheet.Columns[4].ColumnWidth = 3;
Worksheet.Columns[5].ColumnWidth = 2;
Worksheet.Columns[6].ColumnWidth = 13;
Worksheet.Columns[7].ColumnWidth = 15;
Worksheet.Columns[8].ColumnWidth = 4;
Worksheet.Columns[9].ColumnWidth = 8;
Worksheet.Columns[10].ColumnWidth = 6;
Worksheet.Columns[11].ColumnWidth = 10;
Worksheet.Columns[12].ColumnWidth = 18;
Worksheet.Columns[13].ColumnWidth = 16;
Worksheet.Columns[14].ColumnWidth = 18;
Worksheet.Columns[MaxCol + 1].ColumnWidth = 15; //Set the Width of the last Column
for (int b = 2; b <= MaxCol; b++) //If we wanted to include the "Header[14]", we would have to set MaxCol+1, because the Array (not the DataTable) is shifted by +1. These are Excel Worksheet settings!
{
Worksheet.Columns[b].WrapText = true;
//Worksheet.Columns[b].AutoFit();
}
for (int b = 2; b <= MaxCol + 1; b++)
{
Worksheet.Columns[b].Font.Size = 7;
}
Worksheet.PageSetup.Orientation = XlPageOrientation.xlLandscape;
Worksheet.PageSetup.TopMargin = 0.5;
Worksheet.PageSetup.BottomMargin = 0.5;
Worksheet.PageSetup.RightMargin = 0.5;
Worksheet.PageSetup.LeftMargin = 0.5;
// check fielpath
if (ExcelFilePath != null && ExcelFilePath != "")
{
Worksheet.SaveAs(ExcelFilePath);
Excel.Quit();
Marshal.FinalReleaseComObject(Worksheet);
Marshal.FinalReleaseComObject(TitleRange);
Marshal.FinalReleaseComObject(HeaderRange);
Marshal.FinalReleaseComObject(CellRange);
Marshal.FinalReleaseComObject(Excel);
}
else
// no filepath is given
{
Excel.Visible = true;
}
}
}
}
答案 2 :(得分:-1)
workSheet.Cells["A1:T1"].Style.WrapText = true;