Cambiar color de fondo y bordes a una celda de EXCEL
Publicado por Zausen (22 intervenciones) el 27/10/2019 19:54:25
Buenos días,
Estoy haciendo unas pruebas y me he quedado atascado al intentar cambiar el color de fondo de una celda...
No logro encontrar solución, ¿Alguien puede ayudarme? ADjunto TODO el código de prueba.
Un saludo y gracias de antemano.
Estoy haciendo unas pruebas y me he quedado atascado al intentar cambiar el color de fondo de una celda...
No logro encontrar solución, ¿Alguien puede ayudarme? ADjunto TODO el código de prueba.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using System.Collections.Generic;
using System.Linq;
using Excel = Microsoft.Office.Interop.Excel;
namespace AccesoDatos
{
public class Lector
{
public static void Leer()
{
//Preparamos la aplicación.
Excel.Application aplicacion = new Excel.Application();
//Con la aplicación obtenemos el entorno de trabajo, lo que viene a ser el conjunto de páginas.
Excel.Workbook trabajo = aplicacion.Workbooks.Open(@"C:\Users\fermi\Desktop\Proyectos\Lector excel\Seguimiento-INC-PS.xlsx");
try
{
List<Excel.Worksheet> paginas = new List<Excel.Worksheet>();
foreach(Excel.Worksheet p in trabajo.Sheets)
{
paginas.Add(p);
}
bool primerValor = false;
foreach(Excel.Worksheet pagina in paginas)
{
int filas = pagina.Rows.Count;
int columnas = pagina.Columns.Count;
string nombre = pagina.Name;
for(int f = 2; f <= filas & f <= 10; f++)
{
for(int c = 2; c <= columnas & c <= 14; c++)
{
Excel.Range range = (pagina.Cells[f, c] as Excel.Range);
String cellValue = range?.Value?.ToString();
if(cellValue != null)
{
string valorr = cellValue;
string OMC = valorr.ToUpper();
if (!primerValor)
{
//((Excel.Range)pagina.Cells[3, 2]).Value = "PAQUITA";
Excel.Range copyRange = (pagina.Range[pagina.Cells[3, 1], pagina.Cells[3, 14]] as Excel.Range);
//copyRange.Copy(Type.Missing);
//((Excel.Range)pagina.Cells[3, 2]).Value = valorOriginal;
copyRange.Insert(Excel.XlInsertShiftDirection.xlShiftDown);
Excel.Range celda = (Excel.Range)pagina.Cells[3, 2];
String value2 = ((Excel.Range)pagina.Cells[3, 2]).Value2?.ToString();
Excel.Style estilo = (Excel.Style)((Excel.Range)pagina.Cells[3, 2]).Style;
String value = ((Excel.Range)pagina.Cells[3, 2]).Value?.ToString();
String text = ((Excel.Range)pagina.Cells[3, 2]).Text?.ToString();
String numeroFormato = ((Excel.Range)pagina.Cells[3, 2]).NumberFormat?.ToString();
String numeroFormatoLocal = ((Excel.Range)pagina.Cells[3, 2]).NumberFormatLocal?.ToString();
Excel.Font fuente = ((Excel.Range)pagina.Cells[3, 2]).Font;
Excel.Borders bordes = ((Excel.Range)pagina.Cells[3, 2]).Borders;
Boolean equitativo = ((Excel.Style)((Excel.Range)pagina.Cells[3, 2]).Style).AddIndent;
var alineacionH = ((Excel.Style)((Excel.Range)pagina.Cells[3, 2]).Style).HorizontalAlignment;
var alineacionV = ((Excel.Style)((Excel.Range)pagina.Cells[3, 2]).Style).VerticalAlignment;
Boolean tieneBordes = ((Excel.Style)((Excel.Range)pagina.Cells[3, 2]).Style).IncludeBorder;
Boolean textoAjustado = ((Excel.Style)((Excel.Range)pagina.Cells[3, 2]).Style).WrapText;
Excel.Interior interior = ((Excel.Style)((Excel.Range)pagina.Cells[3, 2]).Style).Interior;
//¿Cómo le cambio el color de fondo a la elda 3,2 o B3?
((Excel.Range)pagina.Cells[3, 2]).Value = "2";
primerValor = true;
}
}
String miaumiau = "";
}
int fincolumna = 0;
}
int finPagina = 0;
}
int macarron = 100;
}
catch (Exception ex)
{
String mensaje = ex.Message;
}
finally
{
if (trabajo != null) trabajo.Close(true);
if (aplicacion != null) aplicacion.Quit();
}
}
}
}
Un saludo y gracias de antemano.
Valora esta pregunta


0