PDA

View Full Version : زنگ سلول در خروجی اکسل



hamix666
سه شنبه 01 دی 1394, 20:20 عصر
چطوری می تونم توی سی شارپ رنگ یک سلول از خروجی اکسل رو تغییر بدم؟

حسین.کاظمی
سه شنبه 01 دی 1394, 21:06 عصر
من از این کد استفاده میکنم
ببین کارتو راه میندازه

کد

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using VBIDE_12 = Microsoft.Vbe.Interop;
using Office_12 = Microsoft.Office;
using Excel_12 = Microsoft.Office.Interop.Excel;
using System.Threading;
using System.Globalization;
using System.Drawing;
using Microsoft.Office.Interop.Excel;
using System.Reflection;

namespace SGP
{
public class ExportToExcel1
{


Excel_12.Application oExcel_12 = null; //Excel_12 Application
Excel_12.Workbook oBook = null; // Excel_12 Workbook
Excel_12.Sheets oSheetsColl = null; // Excel_12 Worksheets collection
Excel_12.Worksheet oSheet = null; // Excel_12 Worksheet
Excel_12.Range oRange = null; // Cell or Range in worksheet
Object oMissing = System.Reflection.Missing.Value;
Excel_12.Range oRange1 = null;
public void dataGridView2Excel(DataGridView myDataGridView)
{
try
{
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("en-US");
oExcel_12 = new Excel_12.Application();
oExcel_12.Visible = true;
oExcel_12.UserControl = true;
oBook = oExcel_12.Workbooks.Add(oMissing);
oSheetsColl = oExcel_12.Worksheets;
oSheet = (Excel_12.Worksheet)oSheetsColl.get_Item("Sheet1");
oSheet.DisplayRightToLeft = true;
oRange1 = (Excel_12.Range)oSheet.get_Range("a1", "xfd5000");
oRange1.Font.Name = "B Yekan";
oRange1.Font.Size = "12";



int cnt = 0;
for (int i = 0; i < myDataGridView.Rows.Count; i++)
{
for (int j = 0; j < myDataGridView.ColumnCount; j++)
if (myDataGridView[j, i].Value != null)
{
if (j > cnt)
cnt = j;
}
}

cnt++;
for (int j = 0; j < cnt; j++)
{
oRange = (Excel_12.Range)oSheet.Cells[1, j + 1];
oRange.Value2 = myDataGridView.Columns[j].HeaderText;
oRange.Borders.LineStyle = BorderStyle.Fixed3D;
oRange.Font.Color = 155;
oRange.ColumnWidth = 10;
oRange.Columns.Font.Size = 10;
oRange.Interior.Color = 0xD4D4DE;
}

oSheet.Application.ActiveWindow.SplitRow = 1;
oSheet.Application.ActiveWindow.FreezePanes = true;

for (int i = 0; i < myDataGridView.Rows.Count; i++)
{
for (int j = 0; j < cnt; j++)
{
oRange = (Excel_12.Range)oSheet.Cells[i + 2, j + 1];

oRange.Value2 = myDataGridView[j, i].Value;
oRange.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEd geLeft].Weight = 2;
if (i % 2 == 0)
{
oRange.Interior.Color = 0xF3F3F7;
oRange.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEd geTop].Weight = 2;
}
}
oSheet.Application.ActiveWindow.ScrollRow = i + 1;
}

oBook = null;
oExcel_12 = null;
GC.Collect();
}
catch (Exception)
{
}

}

}
}



شرمنده با آپلود سایت مشکل دارم !!!!