public static bool writeWord(ArrayList alItemName, ArrayList alItemText, string strDemoFile, string strTempFile) { bool flag = false; try { KillProcess("WINWORD"); File.Copy(strDemoFile, strTempFile, true); CCWordApp cw = new CCWordApp(); //System.Threading.Thread.Sleep(1000 * 5 ); if (File.Exists(strTempFile)) { cw.Open(strTempFile); for (int i = 0; i < alItemName.Count && i < alItemText.Count; i++) { cw.ReplaceText(alItemName[i].ToString(), alItemText[i].ToString()); } //for (int i = 0; i < alPicName.Count && i < alPicFilePath.Count; i++) //{ // cw.ReplacePic(alPicName[i].ToString(), alPicFilePath[i].ToString(), alPicWidth[i], alPicHeight[i]); //} cw.Save(); cw.Quit(); cw = null; if (File.Exists(strDemoFile)) { flag = true; } } } catch (Exception e) { Console.WriteLine(e.ToString()); flag = false; } KillProcess("WINWORD"); System.GC.Collect(); return(flag); }
protected void btnCartaOrion_Click(object sender, EventArgs e) { if (Gravar()) { load(); DateTime dated; dated = DateTime.Now; CCWordApp DocWord = new CCWordApp(); try { CultureInfo culture = new CultureInfo("pt-BR"); DateTimeFormatInfo dtfi = culture.DateTimeFormat; int dia = DateTime.Now.Day; int ano = DateTime.Now.Year; string mes = culture.TextInfo.ToTitleCase(dtfi.GetMonthName(DateTime.Now.Month)); string diasemana = culture.TextInfo.ToTitleCase(dtfi.GetDayName(DateTime.Now.DayOfWeek)); string data = diasemana + ", " + dia + " de " + mes + " de " + ano; CNumeroPorExtenso ValorExtenso = new CNumeroPorExtenso(); ValorExtenso.SetNumero(Convert.ToDecimal(txtValorNota.Text.Replace(".", ","))); #region Numerar documento string cmpconumeracaodocumento = ""; tblNumeracaoDocumento tbl = new tblNumeracaoDocumento(); if (string.IsNullOrEmpty(txtNumeroDoc.Text)) { tbl.cmpCoNumeracaoDocumento = "0"; tbl.cmpCoFuncionario = "47"; //Departamento Faturamento tbl.cmpCoObra = "23"; //Escritório GrupoOrion tbl.cmpIdObraFatura = ViewState["cmpIdObraFatura"].ToString(); tbl.cmpCoTipoNumeracao = ViewState["cmpCoTipoNumeracao"].ToString(); tbl.cmpTxObservacoes = "Carta de entrega de NF " + txtNuNotaFiscal.Text.ToString() + " R$ " + txtValorNota.Text.ToString() + " referente a " + ViewState["cmpNuMesFatura"].ToString() + "/" + ViewState["cmpNuAnoFatura"].ToString() + " " + lblNomeObra.Text.ToString(); tbl.cmpDcDocumento = ""; tbl.cmpNoUsuario = ((HzLibGeneral.Util.HzLogin)Session["login"]).cmpNoUsuario.TrimEnd(); txtNumeroDoc.Text = tbl.Save(Global.GetConnection(), ref cmpconumeracaodocumento); } #endregion DocWord.Open(ConfigurationManager.AppSettings["WordMod"] + "Modelo de Carta Orion.dot"); DocWord.GotoBookMark("Numero_Doc"); DocWord.SetAlignment("Right"); DocWord.InsertText(txtNumeroDoc.Text.ToString()); DocWord.SetFont("nothing"); DocWord.GotoBookMark("Data_Carta"); DocWord.SetAlignment("Right"); DocWord.InsertText(data); DocWord.SetFont("nothing"); string NomeDestinatario = ""; if (!string.IsNullOrEmpty(txtNomeDestinatario.Text)) { NomeDestinatario = "Ilmo(a). Sr(a). " + txtNomeDestinatario.Text; } DocWord.GotoBookMark("Nome_Destino"); DocWord.SetFont("Bold"); DocWord.InsertText(NomeDestinatario); DocWord.SetFont("nothing"); DocWord.GotoBookMark("Nome_Orgao"); DocWord.InsertText(txtNomeOrgao.Text); DocWord.SetFont("nothing"); DocWord.GotoBookMark("Data_Inicial"); DocWord.InsertText(txtDataInicial.Text); DocWord.GotoBookMark("Data_Final"); DocWord.InsertText(txtDataFinal.Text); DocWord.GotoBookMark("Numero_Contrato"); DocWord.InsertText(ViewState["cmpNuContrato"].ToString().TrimEnd()); DocWord.GotoBookMark("Data_Emissao_Nota"); DocWord.InsertText(txtEmissaoNota.Text.ToString()); DocWord.GotoBookMark("Tipo_Servico"); DocWord.InsertText(txtNomeServico.Text.ToString()); DocWord.GotoBookMark("Numero_Nota"); DocWord.InsertText(txtNuNotaFiscal.Text); DocWord.GotoBookMark("Valor_Nota"); string ValorNota = txtValorNota.Text.Replace(".", ""); DocWord.InsertText(Convert.ToDecimal(ValorNota).ToString("#,##0.00")); DocWord.GotoBookMark("Valor_NotaExtenso"); DocWord.InsertText(ValorExtenso.ToString()); ArrayList list = new ArrayList(); list.Add("Anexo1"); list.Add("Anexo2"); list.Add("Anexo3"); list.Add("Anexo4"); list.Add("Anexo5"); list.Add("Anexo6"); list.Add("Anexo7"); list.Add("Anexo8"); list.Add("Anexo9"); list.Add("Anexo10"); int i = 0; foreach (ListItem item in lstDocAnexo.Items) { DocWord.GotoBookMark(list[i].ToString()); DocWord.InsertText("-" + item.Text); i++; } DocWord.InsertLineBreak(); DocWord.SaveAs(ConfigurationManager.AppSettings["WordDoc"] + "CO" + txtNumeroDoc.Text.Replace("/", "-") + " " + lblNomeObra.Text.ToString() + ".doc"); DocWord.Quit(); #region Grava nome do arquivo na tabela de documentos if (string.IsNullOrEmpty(txtNumeroDoc.Text)) { tbl.cmpCoNumeracaoDocumento = cmpconumeracaodocumento; tbl.cmpDcDocumento = ConfigurationManager.AppSettings["WordDoc"].ToString() + " CO" + txtNumeroDoc.Text.Replace("/", "-") + " " + lblNomeObra.Text.ToString() + ".doc"; tbl.Save(Global.GetConnection(), ref cmpconumeracaodocumento); } #endregion #region gravar data e número da carta orion tblFaturaObra table = new tblFaturaObra(); table.cmpIdFaturaObra = ViewState["idFaturaObra"].ToString(); table.cmpEdCartaoOrion = ConfigurationManager.AppSettings["WordDoc"].ToString() + " CO" + txtNumeroDoc.Text.Replace("/", "-") + " " + lblNomeObra.Text.ToString() + ".doc"; table.cmpNuCartaOrion = txtNumeroDoc.Text.ToString(); table.cmpNoUsuario = ((HzLibGeneral.Util.HzLogin)Session["login"]).cmpNoUsuario.TrimEnd(); table.GravarCartaOrionFatura(Global.GetConnection()); load(); #endregion Response.Clear(); Response.ContentType = "application/msword"; Response.AddHeader("Content-disposition", "filename=" + ConfigurationManager.AppSettings["WordDoc"] + "CO" + txtNumeroDoc.Text.Replace("/", "-") + " " + lblNomeObra.Text.ToString() + ".doc"); Response.WriteFile(ConfigurationManager.AppSettings["WordDoc"] + "CO" + txtNumeroDoc.Text.Replace("/", "-") + " " + lblNomeObra.Text.ToString() + ".doc"); Response.End(); //Response.Write("<script language='javascript'>" // + "window.open('" + Global.UrlDocWord + "CO" + txtNumeroDoc.Text.Replace("/", "-") + " " + lblNomeObra.Text.ToString() + ".doc" + "', '_blank', 'width=850, height=600, menubar=no, resizable=yes, scrollbars=yes, top=35, left=105')" // + "</script>"); } catch (Exception ex) { DocWord.Quit(); MsgBox.ShowMessage(ex.ToString(), "Erro"); } } }