C # generated by the export Excel files

#region 生成Excel文件
public static void ExportExcel(System.Data.DataTable dt, string strFile, string strMailCode)
{
LogTool lg = new LogTool();
lg.Write("获取DT记录数:" + dt.Rows.Count);
if (dt != null)
{
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();

lg.Write ( "open Excel file ....");
IF (Excel == null) {return;}
excel.Visible = to false; // set invisible, operations are performed in the background, it is true open Excel

//excel.DisplayFullScreen = true; set for full opening // explicit when
Microsoft.Office.Interop.Excel.Workbooks workbooks = excel.Workbooks; // initialize workbook

// add a new workbook, Add () method can also be directly passed parameter to true
Microsoft.Office.Interop.Excel.Workbook Workbook = Workbooks.Add (Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
// same It is a new workbook, but the save dialog box will pop up
//Microsoft.Office.Interop.Excel.Workbook workbook = excel.Application.Workbooks.Add (true);

// Add a new Excel sheet (Sheet)
Microsoft.Office.Interop.Excel.Worksheet Worksheet = (Microsoft.Office.Interop.Excel.Worksheet) workbook.Worksheets [. 1];
worksheet.Name = dt.TableName; // Set name of the table
the try
{
Microsoft.Office.Interop.Excel.Range Range; // Create a cell
int rowIndex = 1; // start index for the row. 1
int = colIndex,. 1; // starting column index 1

for (int i = 0; i <dt.Columns.Count; i ++) // Set the column name
{
// Set the first row, i.e., column names
worksheet.Cells [rowIndex, colIndex + i] = dt.Columns [i] .ColumnName;
// get each cell in the first row
Range = (Microsoft.Office.Interop.Excel.Range) worksheet.Cells [rowIndex, colIndex, + I];
// set the cell interior color
range.Interior. = 0 the ColorIndex; // 16 // 48 // 15
range.Font.Bold = to false; // bold font
range.Font.Color = 0; // set to black
range.Font.Name = "Calibri"; / / font set
range.Font.Size = 10; // set the font size
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; // horizontal center
range.VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign. xlVAlignCenter; // vertically centered
range.ColumnWidth = 12; // set the column width
}

IF (strMailCode == "103")
{
for (int. 6 = Y; Y <dt.Columns.Count; Y ++)
{
worksheet.Cells [rowIndex, colIndex, + Y] = dt.Columns [Y] .ColumnName; // a first row, i.e., the column names
range = (Microsoft.Office.Interop.Excel.Range) worksheet.Cells [rowIndex , colIndex + y]; // get each grid cell of the first row
range.Interior.ColorIndex = 17 ; // 17 // 20 provided inside the cell color
range.ColumnWidth = 24; // set the column width
range.Font.Color = 2;
}
}

rowIndex ++; // skip the first row, the first row is written column name

//写入数据
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
worksheet.Cells[rowIndex + i, colIndex + j] = dt.Rows[i][j].ToString();
}
}
//******************************************************************************************
worksheet.Rows.RowHeight = 16;
worksheet.Rows.Font.Size = 10;
worksheet.Rows.Font.Name = "Calibri";
worksheet.Rows.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;//水平居中
((Microsoft.Office.Interop.Excel.Range)worksheet.Rows["1:1", System.Type.Missing]).RowHeight = 24;// 表头行高

//******************************************************************************************
if (strMailCode == "101")
{
worksheet.get_Range(worksheet.Cells[1, 3], worksheet.Cells[dt.Rows.Count + 1, 3]).ColumnWidth = 9;
worksheet.get_Range(worksheet.Cells[1, 4], worksheet.Cells[dt.Rows.Count + 1, 4]).ColumnWidth = 10;
worksheet.get_Range(worksheet.Cells[1, 2], worksheet.Cells[dt.Rows.Count + 1, 2]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;//水平居左
worksheet.get_Range(worksheet.Cells[1, 4], worksheet.Cells[dt.Rows.Count + 1, 4]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;//水平居左
worksheet.get_Range(worksheet.Cells[1, 12], worksheet.Cells[dt.Rows.Count + 1, 12]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;//水平居左
worksheet.get_Range(worksheet.Cells[1, 13], worksheet.Cells[dt.Rows.Count + 1, 13]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;//水平居左
worksheet.get_Range(worksheet.Cells[1, 14], worksheet.Cells[dt.Rows.Count + 1, 14]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;//水平居左
for (int y = 6; y <= 11; y++)
{
worksheet.get_Range(worksheet.Cells[1, y], worksheet.Cells[dt.Rows.Count + 1, y]).ColumnWidth = 8;
}
worksheet.get_Range(worksheet.Cells[1, 12], worksheet.Cells[dt.Rows.Count + 1, 12]).ColumnWidth = 18;
worksheet.get_Range(worksheet.Cells[1, 13], worksheet.Cells[dt.Rows.Count + 1, 13]).ColumnWidth = 18;
worksheet.get_Range(worksheet.Cells[1, 14], worksheet.Cells[dt.Rows.Count + 1, 14]).ColumnWidth = 18;
((Microsoft.Office.Interop.Excel.Range)worksheet.Rows["1:1", System.Type.Missing]).Font.Bold = true;
}

IF (strMailCode == "102")
{
for (int. 4 = Y; Y <= dt.Columns.Count; Y ++)
{
Range = (Microsoft.Office.Interop.Excel.Range) worksheet.Cells [. 1, Y] ; // get each grid cell of the first row
range.Interior.ColorIndex = 15; // 17 // 20 provided inside the cell color
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; // horizontal center
}

lg.Write("Excel第一列合并单元格....");
string strBeginArea = "";
for (int i = 1; i < dt.Rows.Count + 1; i++)
{
string temp = ((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[i, 1]).Text.ToString().Trim();
if (strBeginArea == temp)
{
Microsoft.Office.Interop.Excel.Range colMer = worksheet.get_Range(worksheet.Cells[i - 1, 1], worksheet.Cells[i, 1]);
colMer.Application.DisplayAlerts = false;
colMer.Merge(System.Type.Missing);
colMer.Application.DisplayAlerts = true;
}
strBeginArea = temp;
}
worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[dt.Rows.Count + 1, 1]).ColumnWidth = 10;
worksheet.get_Range(worksheet.Cells[1, 2], worksheet.Cells[dt.Rows.Count + 1, 2]).ColumnWidth = 18;
worksheet.get_Range(worksheet.Cells[1, 2], worksheet.Cells[dt.Rows.Count + 1, 2]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;//水平居左
((Microsoft.Office.Interop.Excel.Range)worksheet.Rows["1:1", System.Type.Missing]).Font.Bold = true;
}
if (strMailCode == "104")
{
((Microsoft.Office.Interop.Excel.Range)worksheet.Rows["1:1", System.Type.Missing]).Font.Bold = true;
worksheet.get_Range(worksheet.Cells[2, 1], worksheet.Cells[dt.Rows.Count + 1, 1]).NumberFormatLocal = "yyyy-mm";
worksheet.get_Range(worksheet.Cells[2, 2], worksheet.Cells[dt.Rows.Count + 1, 2]).NumberFormatLocal = "000000";
worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[dt.Rows.Count + 1, 1]).ColumnWidth = 8;
worksheet.get_Range(worksheet.Cells[1, 2], worksheet.Cells[dt.Rows.Count + 1, 2]).ColumnWidth = 8;
worksheet.get_Range(worksheet.Cells[1, 3], worksheet.Cells[dt.Rows.Count + 1, 3]).ColumnWidth = 32;
worksheet.get_Range(worksheet.Cells[1, 8], worksheet.Cells[dt.Rows.Count + 1, 8]).ColumnWidth = 6;
worksheet.get_Range(worksheet.Cells[1, 13], worksheet.Cells[dt.Rows.Count + 1, 13]).ColumnWidth = 9;
worksheet.get_Range(worksheet.Cells[1, 3], worksheet.Cells[dt.Rows.Count + 1, 3]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;//水平居左
worksheet.get_Range(worksheet.Cells[1, 5], worksheet.Cells[dt.Rows.Count + 1, 5]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;//水平居左
worksheet.get_Range(worksheet.Cells[1, 7], worksheet.Cells[dt.Rows.Count + 1, 7]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignRight;//水平居右
worksheet.get_Range(worksheet.Cells[1, 9], worksheet.Cells[dt.Rows.Count + 1, 9]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;//水平居左
worksheet.get_Range(worksheet.Cells[1, 10], worksheet.Cells[dt.Rows.Count + 1, 10]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;//水平居左
worksheet.get_Range(worksheet.Cells[1, 11], worksheet.Cells[dt.Rows.Count + 1, 11]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;//水平居左
worksheet.get_Range(worksheet.Cells[1, 12], worksheet.Cells[dt.Rows.Count + 1, 12]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;//水平居左
//Microsoft.Office.Interop.Excel.Range colMer = worksheet.get_Range(worksheet.Cells[2, 1], worksheet.Cells[dt.Rows.Count + 1, 1]);
//colMer.NumberFormatLocal = "yyyy-mm";
}

// if prompted, if you want to delete a sheet page, first of all to this set fasle.
= false excel.DisplayAlerts;
workbook.Saved = to true; // save the data written, there has not been saved to disk
// Create a file
FileStream file = new FileStream (strFile, FileMode.CreateNew);

// close the release flow, or no way to write data
File.close ();
file.Dispose ();

// save to the specified path
workbook.SaveCopyAs (strFile);
}
the catch (Exception ERR)
{
lg.Write ( "\ n-wrong:" + err.message);
}
the finally
{
workbook.Close (to false, Type.Missing , Type.Missing);
workbooks.Close ();
excel.Quit (); // close the exit

worksheet = null;
workbook = null;
workbooks = null;
excel = null;

GC.Collect();
}
}
}
#endregion

Guess you like

Origin www.cnblogs.com/812FC/p/12167602.html