Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Help with Microsoft.Office.Interop and c#..........

Hello, please i need help, I need to create an excel file that contains a
column of cells with data validation type list, as I do this? I tried using
"xlsRange.FormulaLabel" and "xlsWorkbook.AcceptLabelsInFormulas = true"
because my cell I need the list in another excel sheet but I always see an
error.

Here my code:
public void ConvertXMLToExcel(string strFileName)
{
int vcont=1;
int vrow = 1;
int vcol = 0;
int vsheet = 0;
int IdProcessExcell = 0;
XDocument xmldoc = XDocument.Load(strFileName);
try
{
if (xmldoc.Elements().Count() 0)
{
xlsApp = new
Microsoft.Office.Interop.Excel.ApplicationClass();

IdProcessExcell=System.Diagnostics.Process.GetProc essesByName("Excel")[0].Id;
xlsApp.DisplayAlerts = false;

foreach (XElement XLibro in
xmldoc.Element("Excel").Elements("Libro"))
{
xlsWorkbook = xlsApp.Workbooks.Add(true);
//xlsWorkbook.AcceptLabelsInFormulas = true;
foreach (XElement XHoja in XLibro.Elements())
{
vsheet = vsheet + 1;
if (vsheet 1)
xlsWorksheet =
(Microsoft.Office.Interop.Excel.Worksheet)xlsWorkb ook.Worksheets.Add(Type.Missing, Type.Missing, 1, Microsoft.Office.Interop.Excel.XlSheetType.xlWorks heet);
else
xlsWorksheet =
(Microsoft.Office.Interop.Excel.Worksheet)(xlsWork book.Worksheets[vsheet]);
xlsWorksheet.Activate();
xlsWorksheet.Name =
XHoja.FirstAttribute.Value;
foreach (XElement XHojaColumna in
XHoja.Elements("Campos"))
{
vlist = new List<List<string();
foreach (XElement XCampos in
XHojaColumna.Elements("Campo"))
{

foreach (XAttribute XAtributte in
XCampos.Attributes("Campo"))
{
xlsWorksheet.Cells[1, vcont] =
XAtributte.Value;
vcont = vcont + 1;
}
foreach (XAttribute XAtributte in
XCampos.Attributes("ConAyuda"))
{
vNodelist = new List<string();

if (XAtributte.Value=="1")
{
foreach (XElement
XCampoAyuda in XCampos.Elements("Ayuda"))
{

vNodelist.Add(XCampoAyuda.Attribute("Valor").Value );

}
}
}
vlist.Add(vNodelist);
}
vcont = 1;
}
vNodeListIndex=0;
foreach (XElement XHojaFila in
XHoja.Elements("Renglon"))
{
vrow = vrow + 1;
foreach (XElement XCampos in
XHojaFila.Elements("Celda"))
{
foreach (XAttribute XAtributte in
XCampos.Attributes("Valor"))
{
vcol = vcol + 1;
xlsWorksheet.Cells[vrow, vcol] =
XAtributte.Value;
}
if ( vlist[vNodeListIndex].Count0 )
{
CellWithList(vrow, vcol,
vlist[vNodeListIndex], xlsWorkbook, xlsWorksheet);

}
vNodeListIndex = vNodeListIndex + 1;
}
vNodeListIndex = 0;
vcol = 0;
}
vrow = 1;
releaseObject(xlsWorksheet);
}
if
(File.Exists(Path.GetDirectoryName(strFileName) +
XLibro.Attribute("Archivo").Value))
{

File.Delete(Path.GetDirectoryName(strFileName) +
XLibro.Attribute("Archivo").Value);
}
xlsWorkbook.Close(true,
Path.GetDirectoryName(strFileName) + XLibro.Attribute("Archivo").Value,
null);
releaseObject(xlsWorkbook);
}
xlsApp.Quit();

System.Diagnostics.Process.GetProcessById(IdProces sExcell).Kill();
}
}
catch (Exception ex)
{
string msgerror = ex.Message;
}
finally
{
releaseObject(xlsApp);
}

}
private void CellWithList(int vrow, int vcol, List<String
vTmpNodelist, Microsoft.Office.Interop.Excel.Workbook
xlsWorkbook,Microsoft.Office.Interop.Excel.Workshe et xlsWorksheet)
{
Microsoft.Office.Interop.Excel.Worksheet xlsTmpWorksheet;
xlsTmpWorksheet =
(Microsoft.Office.Interop.Excel.Worksheet)xlsWorkb ook.Worksheets.Add(Type.Missing, Type.Missing, 1, Microsoft.Office.Interop.Excel.XlSheetType.xlWorks heet);
xlsTmpWorksheet.Name =
Guid.NewGuid().GetHashCode().ToString("X");

Microsoft.Office.Interop.Excel.Range xlsRange;

xlsRange = xlsWorksheet.get_Range(xlsWorksheet.Cells[vrow,
vcol], xlsWorksheet.Cells[vrow, vcol]);

int vtmprow = 2;
foreach (string vvalue in vTmpNodelist)
{
vtmprow = vtmprow + 1;
xlsTmpWorksheet.get_Range(xlsTmpWorksheet.Cells[2,
vcol], xlsTmpWorksheet.Cells[vtmprow, vcol]).Value2 = vvalue;
//xlsWorksheet.get_Range(xlsWorksheet.Cells[vtmprow,
vcol], xlsWorksheet.Cells[vtmprow, vcol]).Value2 = vvalue;
}
//xlsRange.Select();
xlsRange.FormulaLabel =
Microsoft.Office.Interop.Excel.XlFormulaLabel.xlCo lumnLabels ;//"abc"; //
xlsTmpWorksheet.Name;
xlsRange.Name = "aaa";
string values = "";
//values = "=" +
xlsWorksheet.get_Range(xlsWorksheet.Cells[30, vcol],
xlsWorksheet.Cells[vtmprow, vcol]).get_Address(Type.Missing, Type.Missing,
Microsoft.Office.Interop.Excel.XlReferenceStyle.xl A1, Type.Missing,
Type.Missing);
//values = "=" +
xlsTmpWorksheet.get_Range(xlsTmpWorksheet.Cells[1, vcol],
xlsTmpWorksheet.Cells[vtmprow, vcol]).get_Address(Type.Missing, Type.Missing,
Microsoft.Office.Interop.Excel.XlReferenceStyle.xl A1, Type.Missing,
Type.Missing);
values = "=" + xlsRange.Name ;

xlsRange.Validation.Add(Microsoft.Office.Interop.E xcel.XlDVType.xlValidateList,
Microsoft.Office.Interop.Excel.XlDVAlertStyle.xlVa lidAlertStop,
Microsoft.Office.Interop.Excel.XlFormatConditionOp erator.xlBetween, values,
Type.Missing);
}
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to set the ActivePrinter Property for Microsoft.Office.Interop Vinu Excel Programming 0 August 26th 09 02:06 PM
Microsoft.Office.Interop.Excel.Application Andie Excel Programming 1 May 22nd 09 04:27 AM
Namespace Microsoft.Office.Interop Mojtaba Excel Programming 1 June 20th 07 01:25 PM
Microsoft.Office.Interop.Excel Texttocolumns C# Cheryl[_5_] Excel Programming 1 April 6th 07 08:40 PM
COMException With Microsoft.Office.Interop.Excel frankvfox Excel Programming 4 September 27th 06 04:36 PM


All times are GMT +1. The time now is 07:45 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"