C# Excel中列的WrapText(以编程方式)

C# Excel中列的WrapText(以编程方式),c#,excel,office-interop,C#,Excel,Office Interop,我正在尝试使用C#将WrapText属性设置为true 但是它没有任何例外。发生了什么?谢谢大家! 我在这里复制了大量的代码,对于我的整个解决方案,TextWrap就是其中之一。我想有一天它可能会对某人有用 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Excel = Microsoft.Office

我正在尝试使用C#将WrapText属性设置为true


但是它没有任何例外。发生了什么?谢谢大家!

我在这里复制了大量的代码,对于我的整个解决方案,
TextWrap
就是其中之一。我想有一天它可能会对某人有用

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);
主程序:

    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;
    }
类别:

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;
        }
    }
}
}
使用Microsoft.Office.Interop.Excel;
使用制度;
使用System.Runtime.InteropServices;
名称空间SAVETOEXCELPROGRAM
{
公共静态类ExportToExcel
{
公共静态void ExportPartsToExcel(此System.Data.DataTable DataTable,字符串ExcelFilePath=null)
{
国际专栏;
int-RowShift=7;
ColumnsCount=DataTable.Columns.Count;
//加载excel,并创建新工作簿
应用程序Excel=新应用程序();
Excel.Workbooks.Add();
//单一工作表
_工作表=Excel.ActiveSheet;
Excel.Sheets[1].Name=“任意”;
Worksheet.Columns.NumberFormat=“@”//强制使用“文本”格式,以便1/2不会转换为例如2月1日
Worksheet.Columns.HorizontalAlignment=XlHAlign.xlHAlignLeft;//文本对齐
Worksheet.Columns.verticalignment=XlHAlign.xlHAlignCenter;//文本对齐
object[,]Title=新对象[5,1];//数组范围从[1,1]开始,内容索引从[0,0]开始
if(GlobalStrings.building\u alternative\u addresses.Length==0)
{
如果(GlobalStrings.building_postcode.Length!=0)
{
标题[0,0]=“日期:”+DateTime.Now.ToString(“dd.MM.yyyy-HH:MM”);
标题[2,0]=“地址:”+GlobalStrings.building\u house\u street+“+GlobalStrings.building\u house\u编号+GlobalStrings.building\u id+”,“+GlobalStrings.building\u邮政编码+”+GlobalStrings.building\u区域;
标题[3,0]=“C.C.:“+GlobalString.building_地籍_community+”,建筑编号:“+GlobalString.building_building_number+”,地块编号:“+GlobalString.building_地块编号”;
}
其他的
{
标题[0,0]=“日期:”+DateTime.Now.ToString(“dd.MM.yyyy-HH:MM”);
标题[2,0]=“地址:”+GlobalStrings.building\u house\u street+”+GlobalStrings.building\u house\u编号+GlobalStrings.building\u house\u id;
标题[3,0]=“C.C.:“+GlobalString.building_地籍_community+”,建筑编号:“+GlobalString.building_building_number+”,“+GlobalString.building_plot_number”;
}
}
其他的
{
如果(GlobalStrings.building_postcode.Length!=0)
{
标题[0,0]=“日期:”+DateTime.Now.ToString(“dd.MM.yyyy-HH:MM”);
标题[2,0]=“地址:”+GlobalStrings.building\u house\u street+“+GlobalStrings.building\u house\u编号+GlobalStrings.building\u id+”,“+GlobalStrings.building\u邮政编码+”+GlobalStrings.building\u区域;
标题[3,0]=“C.C.:“+GlobalString.building_地籍_community+”,建筑编号:“+GlobalString.building_building_number+”,地块编号:“+GlobalString.building_地块编号”;
Title[4,0]=“外观:”+GlobalStrings.building\u alternative\u地址;
}
其他的
{
标题[0,0]=“日期:”+DateTime.Now.ToString(“dd.MM.yyyy-HH:MM”);
标题[2,0]=“地址:”+GlobalStrings.building\u house\u street+”+GlobalStrings.building\u house\u编号+GlobalStrings.building\u house\u id;
标题[3,0]=“C.C.:“+GlobalString.building_地籍_community+”,建筑编号:“+GlobalString.building_building_number+”,地块编号:“+GlobalString.building_地块编号”;
Title[4,0]=“外观:”+GlobalStrings.building\u alternative\u地址;
}
}
范围TitleRange=工作表.get_范围((范围)(工作表.Cells[5,1]),(范围)(工作表.Cells[1,1]);
TitleRange.Value=标题;
TitleRange.Font.Bold=true;
TitleRange.Font.Size=10;
object[]头=新对象[14];//头数
标题[0]=“部分”;
标题[1]=“共享”;
标题[2]=“CRP”;
标题[3]=“+/-”;
标题[4]=“所有者”;
标题[5]=“地址”;
标题[6]=“邮政编码”;
标题[7]=“区域”;
标题[8]=“国家”;
标题[9]=“社会”;
标题[10]=“类型”;
标题[11]=“说明”;
标题[12]=“注释”;
标题[13]=“代码”;
int MaxCol=标题长度;
Range HeaderRange=Worksheet.get_Range((Range)(Worksheet.Cells[RowShift,2]),(Range)(Worksheet.Cells[RowShift,MaxCol+1]);//MaxCol+1,因为我们必须将数组位置移动1,因为第一列设置为“ColumnWidth=0.1”
HeaderRange.Value=表头;
headerrage.Font.Bold=true;
headerrage.Interior.Color=System.Drawing.colorplator.ToOle(System.Drawing.Color.LightGray);
headerrage.Borders.LineStyle=XlLineStyle.xlContinuous;
//数据单元
int rowscont=DataTable.Rows.Count;
object[,]Cells=新对象[RowsCount,ColumnsCount];//数组范围从[1,1]开始,内容索引从[0,0]开始
对于(int j=0;jfor(int i=3;我不确定这是否可行,但可能在调用
AutoFit()之前设置
WrapText
?您好,请看一下这篇文章,希望这有助于@RamRS是正确的,如果您希望列中的宽度保持不变,请设置大小并将wrapText=true,但删除Autofits请添加一些注释,说明您的代码如何解决问题,以改进此答案。
    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;
    }
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;
        }
    }
}
}