using System;
using Excel=Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace MailSend.classFile
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// GetExcel の概要の説明です。
/// </summary>
public class GetExcel
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private DataTable _table;
private string _fileName;
public GetExcel()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
public GetExcel(DataTable tb,string FileName)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_table=tb;
_fileName=FileName;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Down_Excel()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(System.IO.File.Exists(_fileName))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.IO.File.Delete(_fileName);
}
int rowCount=_table.Rows.Count;
int coulumnCount=_table.Columns.Count;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Excel.Application xl=new Excel.Application();
Excel.Workbook xbook=xl.Application.Workbooks.Add(true);
//xl.Application.Workbooks.Add(true);
Excel.Worksheet excelWorkSheet = (Excel.Worksheet)xbook.Worksheets.Add(Missing.Value, Missing.Value, 1, Missing.Value);
switch(classFile.Field.MailFlg)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case "1":
excelWorkSheet.Name = DateTime.Today.ToString("yyyyMMdd")+"ストコン⇔本社売上⇔現金管理";
break;
case "2":
//excelWorkSheet.Name = DateTime.Today.ToString("yyyyMMdd")+"ストコン⇔本社売上⇔現金管理";
break;
case "3":
excelWorkSheet.Name = DateTime.Today.ToString("yyyyMMdd")+"売上ヘッダー明細整合";
break;
default:
//MessageBox.Show("起動バラメータがエラー。");
//Application.Exit();
break;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int rowIndex = 2;
int colIndex = 1;
foreach(DataColumn col in _table.Columns)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
colIndex++;
Excel.Range cell=(Excel.Range)excelWorkSheet.Cells[2,colIndex];
cell.Value2 = col.ColumnName;
cell.Font.ColorIndex=5; //3 red 5 blue 7 pink
}
// foreach(DataRow row in _table.Rows)
// {
// rowIndex++;
// colIndex = 1;
// foreach(DataColumn col in _table.Columns)
// {
// colIndex++;
// excelWorkSheet.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString();
// }
// }
rowIndex = 0;
colIndex = 0;
string [,] array=new string[rowCount,coulumnCount];
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
foreach(DataRow row in _table.Rows)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
foreach(DataColumn col in _table.Columns)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
array[rowIndex,colIndex]=row[col.ColumnName].ToString();
colIndex++;
}
rowIndex++;
colIndex = 0;
}
}
catch(Exception ex)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox.Show(ex.Message);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Excel.Range dataRange=excelWorkSheet.get_Range(excelWorkSheet.Cells[3,2],excelWorkSheet.Cells[rowCount+2,coulumnCount+1]);
dataRange.Value2=array;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Excel.Range select=excelWorkSheet.get_Range(excelWorkSheet.Cells[2,2],excelWorkSheet.Cells[2,coulumnCount+1]);
select.Interior.ColorIndex=6;//backgroud
select.EntireColumn.AutoFit();
select.EntireRow.AutoFit();
select.HorizontalAlignment=Excel.XlHAlign.xlHAlignCenter;
select.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter; //横
select.Borders.LineStyle = BorderStyle.None;
select.Borders.Weight = Excel.XlBorderWeight.xlThin;
select.Borders.ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
select=excelWorkSheet.get_Range(excelWorkSheet.Cells[3,2],excelWorkSheet.Cells[rowCount+2,4]);
select.EntireColumn.AutoFit();
select.EntireRow.AutoFit();
select.HorizontalAlignment=Excel.XlHAlign.xlHAlignLeft;
select.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
select.Borders.LineStyle = BorderStyle.None;
select.Borders.Weight = Excel.XlBorderWeight.xlThin;
select.Borders.ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
select=excelWorkSheet.get_Range(excelWorkSheet.Cells[3,4],excelWorkSheet.Cells[rowCount+2,coulumnCount+1]);
select.EntireColumn.AutoFit();
select.EntireRow.AutoFit();
select.HorizontalAlignment=Excel.XlHAlign.xlHAlignRight;
select.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
select.Borders.LineStyle = BorderStyle.None;
select.Borders.Weight = Excel.XlBorderWeight.xlThin;
// select.Borders.ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;
xl.Visible = false;
xbook.Saved = true;
xl.DisplayAlerts=false;
xbook.SaveCopyAs(_fileName);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//xl.Visible = false;
// if(System.IO.File.Exists(FilePath))
// {
// System.IO.File.Delete(FilePath);
// }
//xl.DisplayAlerts=false;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Excel.Worksheet xSheet=(Excel.Worksheet)xbook.Sheets[1];
// xbook.SaveAs(FilePath,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Excel.XlSaveAsAccessMode.xlNoChange,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);
//
//xl.ActiveWorkbook.SaveAs(FilePath,Excel.XlFileFormat.xlExcel9795,null,null,false,false,Excel.XlSaveAsAccessMode.xlNoChange,null,null,null,null,null);
xl.Quit();
excelWorkSheet=null;
xbook=null;
xl=null;
select=null;
GC.Collect(); // ゴミ回収
//}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
posted @
2008-05-12 12:50
TerryLove
阅读(
174)
评论()
编辑
收藏
举报