add referent
com->microsoft office 12.0 Object Libary
net ->mircrosoft office interrop.excel
###############################################################33
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
// --- export เป็นไฟล์ Excel --------
private void mergeCell(string incell, ref Excel.Application excelApp)
{
excelApp.get_Range(incell, Type.Missing).Merge(Type.Missing);
}
private void bt_excel_Click(object sender, EventArgs e)
{
saveFileDialog1.Filter = "Excel File(*.xls)|*.xls";
saveFileDialog1.ShowDialog();
Excel.Range oResizeRange;
Excel.Application xlApp ;
Excel.Workbook xlWorkBook ;
Excel.Worksheet xlWorkSheet ;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
//----------------- หัวตาราง ---------------------------
xlWorkSheet.Cells[1, 1] = "ข้อมูล แผนก: " + dgv_data[11, 0].Value.ToString() + " หน่วยรถ: " + dgv_data[12, 0].Value.ToString(); ;
xlWorkSheet.Cells[2, 1] = "พื้นที่ จังหวัด: " + dgv_data[7, 0].Value.ToString() + " อำเภอ: " + dgv_data[6, 0].Value.ToString() + " ตำบล: " + dgv_data[5, 0].Value.ToString();
//--------ตัวหนา --------
xlApp.get_Range("A1", "D1").Font.Bold = true;
xlApp.get_Range("A4", "M4").Font.Bold = true;
//-----ขนาดคอลัม ------------
xlApp.get_Range("A4","A4").ColumnWidth = 20;
xlApp.get_Range("B4", "B4").ColumnWidth = 15;
xlApp.get_Range("E4", "F4").ColumnWidth = 15;
xlApp.get_Range("G4", "G4").ColumnWidth = 15;
xlApp.get_Range("J4", "K4").ColumnWidth = 15;
//------marge cell --------
mergeCell("A1:M1,A2:M2",ref xlApp);
//------จัดกลาง---------
xlApp.get_Range("A4", "M1").HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;
xlApp.get_Range("A2", "M2").HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;
xlWorkSheet.Cells[4, 1] = "ชื่อร้าน";
xlWorkSheet.Cells[4, 2] = "ผู้ติดต่อ";
xlWorkSheet.Cells[4, 3] = "เลขที่";
xlWorkSheet.Cells[4, 4] = "หมู่ที่";
xlWorkSheet.Cells[4, 5] = "ถนน";
xlWorkSheet.Cells[4, 6] = "ตำบล";
xlWorkSheet.Cells[4, 7] = "อำเภอ";
xlWorkSheet.Cells[4, 8] = "จังหวัด";
xlWorkSheet.Cells[4, 9] = "รหัสไปรษณีย์";
xlWorkSheet.Cells[4, 10] = "เบอร์ร้าน";
xlWorkSheet.Cells[4, 11] = "มือถือ";
xlWorkSheet.Cells[4, 12] = "Day Trip";
xlWorkSheet.Cells[4, 13] = "วันเยี่ยม";
//---- ตีเส้น -----------------------------------------------
oResizeRange = xlApp.get_Range("a4", "a" + (dgv_data.RowCount + 4).ToString()).get_Resize(Missing.Value, 13);
oResizeRange.Borders.Weight = Excel.XlBorderWeight.xlThin;
// ----------------------------cell value format string ----------------------------
xlApp.get_Range("J5", "J"+(dgv_data.RowCount + 4).ToString()).NumberFormat = "@";
xlApp.get_Range("K5", "K" + (dgv_data.RowCount + 4).ToString()).NumberFormat = "@";
xlApp.get_Range("C5", "C" + (dgv_data.RowCount + 4).ToString()).NumberFormat = "@";
//--------------------------------------------------------
// xlApp.get_Range("A1:A360,B1:E1", Type.Missing).Merge(Type.Missing);
//---------------------------------------------------
int i = 0;
int j = 0;
for (i = 0; i <= dgv_data.RowCount - 1; i++)
{
xlApp.get_Range("A"+(i+5).ToString(), "M"+(i+5).ToString()).HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft;
for (j = 0; j <= dgv_data.ColumnCount - 1; j++)
{
DataGridViewCell cell = dgv_data[j, i];
if ((j != 11) && (j != 12))
{
if ((cell.Value.ToString() == "-") || (cell.Value.ToString() == "0"))//ตรวจสอบข้อมูลที่ขาด
{
xlWorkSheet.Cells[i + 5, j + 1] = "";
}
else
{
xlWorkSheet.Cells[i + 5, j + 1] = cell.Value;
}
}
}
}
xlWorkBook.SaveAs(saveFileDialog1.FileName,Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
MessageBox.Show("Excel file created");
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
//--------------------------------------------------- end excel -------------------------------------------
ไม่มีความคิดเห็น:
แสดงความคิดเห็น