-2
I would like to know how I can format the cells of a spreadsheet by C#, already managed to insert the data I would like in it, but I would like to insert formatting and formulas as well.
Code: `
int i = 2;
String answer = Interaction.InputBox("Digite o nome do arquivo de relatorio", "", null, -1, -1);
while (answer == null)
{
answer = Interaction.InputBox("Digite o nome do arquivo de relatorio", "", null, -1, -1);
}
// Inicia o componente Excel
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
//Cria uma planilha temporária na memória do computador
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
//incluindo os títulos de cada coluna
xlWorkSheet.Cells[1, 1] = "Id Venda";
xlWorkSheet.Cells[1, 2] = "Classificação";
xlWorkSheet.Cells[1, 3] = "Cliente";
xlWorkSheet.Cells[1, 4] = "Cpf/Cnpj";
xlWorkSheet.Cells[1, 5] = "Carro";
xlWorkSheet.Cells[1, 6] = "Placa";
xlWorkSheet.Cells[1, 7] = "Serviço";
xlWorkSheet.Cells[1, 8] = "Preço";
xlWorkSheet.Cells[1, 9] = "Valor Cobrado";
xlWorkSheet.Cells[1, 10] = "Data";
xlWorkSheet.Cells[1, 11] = "Pagamento";
xlWorkSheet.Cells[1, 12] = "Forma de Pagamento";
Classes.VendasServicos vs = new Classes.VendasServicos();
DateTime dataI = DtInicialVenda.Value;
DateTime dataF = dtFinalVenda.Value;
// Incluindo os dados das linhas
con.conectar();
SqlDataReader reader;
if (rdbAmbos.Checked == true)
{
if (cmbServico.Text == "")
{
reader = con.exeCliente($"SELECT [VendasServicos].[idVenda], [Cliente].[frotista], [Cliente].[nome] as 'Cliente', [Cliente].[pfpj], [Vendas].[carro], [Vendas].[placa], [Servicos].[nome] as 'Serviço', [Servicos].[preco], [VendasServicos].[valorCobrado], [Vendas].[data], [Vendas].[pago], [Vendas].[formaPagamento] FROM [VendasServicos] INNER JOIN Vendas ON ([VendasServicos].[idVenda] = [Vendas].[idVenda])INNER JOIN Cliente ON Vendas.idCliente = Cliente.idCliente INNER JOIN Servicos ON [VendasServicos].idServico = Servicos.idServico WHERE [Vendas].[data] BETWEEN '{dataI}' AND '{dataF}' ");
if (reader.HasRows)
{
while (reader.Read())
{
xlWorkSheet.Cells[i, 1] = reader.GetInt32(0);
Boolean tipo = reader.GetBoolean(1);
if (tipo == true)
{
xlWorkSheet.Cells[i, 2] = "Frotista";
}
else
{
xlWorkSheet.Cells[i, 2] = "Particular";
}
xlWorkSheet.Cells[i, 3] = reader.GetString(2).Trim();
try
{
xlWorkSheet.Cells[i, 4] = reader.GetString(3).Trim();
}
catch
{
xlWorkSheet.Cells[i, 4] = "";
}
xlWorkSheet.Cells[i, 5] = reader.GetString(4);
xlWorkSheet.Cells[i, 6] = reader.GetString(5);
xlWorkSheet.Cells[i, 7] = reader.GetString(6);
xlWorkSheet.Cells[i, 8] = reader.GetDouble(7);
try
{
xlWorkSheet.Cells[i, 9] = reader.GetDouble(8);
}
catch
{
xlWorkSheet.Cells[i, 9] = reader.GetDouble(7);
}
xlWorkSheet.Cells[i, 10] = reader.GetDateTime(9);
Boolean pg = reader.GetBoolean(10);
if (pg == true)
{
xlWorkSheet.Cells[i, 11] = "PG";
}
else
{
xlWorkSheet.Cells[i, 11] = "EM ABERTO";
}
try
{
xlWorkSheet.Cells[i, 12] = reader.GetString(11);
}
catch (Exception)
{
}
i++;
}
int lastCell = i - 1;
xlWorkSheet.Cells[i, 8] = "Total:";
xlWorkSheet.Cells[i, 9] = "=SOMA(I2:I" + lastCell + ")";
reader.Close();
con.desconectar();
}
//Salva o arquivo de acordo com a documentação do Excel.
try
{
xlWorkBook.SaveAs(answer, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue,
Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
//o arquivo foi salvo na pasta Meus Documentos.
string caminho = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
//MessageBox.Show("Concluído. Verifique em " + caminho + "arquivo.xls");
try
{
FileInfo fi = new FileInfo($@"{caminho}/{answer}.xls");
if (fi.Exists)
{
System.Diagnostics.Process.Start($@"{caminho}/{answer}.xls");
}
else
{
MessageBox.Show("Arquivo não encontrado");
}
}
catch
{
MessageBox.Show("Test");
}
}
catch
{
MessageBox.Show("Não foi possivel salvar");
}
}`