Sto creando una piccola app che legge un file di testo delimitato da tabulazioni, apporta alcune modifiche e quindi crea un file .xlsx di Excel 2007. Ho difficoltà a capire come prendere le linee da una serie di stringhe e scriverle nel file Excel, usando le schede per suddividere la linea in colonne. Spero che abbia senso.Come posso scrivere una serie di stringhe su file Excel, con un delimitatore di tabulazione?
ho string Lines[]
che contiene qualcosa di simile:
Item1\tItem2\tItem3\tItem4
ItemA\tItemB\tItemC\tItemD
Item5\tItem6\tItem7\tItem8
mi piacerebbe creare un file Excel che assomiglia a questo:
A B C D
Item1 Item2 Item3 Item4
ItemA ItemB ItemC ItemD
Item5 Item6 Item7 Item8
ho provato quanto segue, ma semplicemente mette la prima riga da Lines[]
in ogni riga e non si separa in colonne:
string Lines[] = GetLines();
Excel.Application xlApp;
Excel.Workbook xlWb;
Excel.Worksheet xlWs;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.Application();
xlWb = xlApp.Workbooks.Add(misValue);
xlWs = (Excel.Worksheet)xlWb.Worksheets.get_Item(1);
Excel.Range c1 = (Excel.Range)xlWs.Cells[2, 1];
Excel.Range c2 = (Excel.Range)xlWs.Cells[2 + lines.Length, 1];
Excel.Range range = xlWs.get_Range(c1, c2);
range.Value = lines;
range.TextToColumns(
range,
Microsoft.Office.Interop.Excel.XlTextParsingType.xlDelimited,
Microsoft.Office.Interop.Excel.XlTextQualifier.xlTextQualifierNone,
false,
true // This is flag to say it is tab delimited
);
xlApp.Visible = true;
Qualche consiglio sarebbe apprezzato! Grazie!
ecco l'output Attualmente sto ricevendo:
A B C D
Item1\tItem2\tItem3\tItem4
Item1\tItem2\tItem3\tItem4
Item1\tItem2\tItem3\tItem4
EDIT: Ho aggiornato il mio codice con il suggerimento di @ jiverson e la linea è ora divisa in colonne in Excel, ma la prima linea da Lines[]
appare ancora in ogni riga in Excel. Perché?
EDIT # 2: Ecco il codice di lavoro aggiornato:
Excel.Application xlApp;
Excel.Workbook xlWb;
Excel.Worksheet xlWs;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.Application();
xlWb = xlApp.Workbooks.Add(misValue);
xlWs = (Excel.Worksheet)xlWb.Worksheets.get_Item(1);
int currentRow = 2;
string[] lines = GetLines();
for (int i = 0; i < lines.Length; i++)
{
string line = lines[i]; //get the current line
string[] values = line.Split('\t'); //split the line at the tabs
//
// .. i do some things to specific values here ..
//
lines[i] = String.Join("\t", values); //put the updated line back together
Excel.Range currentRange = (Excel.Range)xlWs.Cells[currentRow, 1]; //get the next row
currentRange.Value = lines[i]; //write the line to Excel
currentRow++;
}
Excel.Range c1 = (Excel.Range)xlWs.Cells[2, 1]; //get the first cell
Excel.Range c2 = (Excel.Range)xlWs.Cells[2 + lines.Length, 1]; //get the last cell
Excel.Range range = xlWs.get_Range(c1, c2); //set the range as the used area
range.TextToColumns(//split the row into columns
range,
Excel.XlTextParsingType.xlDelimited,
Excel.XlTextQualifier.xlTextQualifierNone,
false,
true // This is flag to say it is tab delimited
);
Dividere la stringa usando ''\ t'' e scrivere ogni valore in ogni cella corrispondente. – chancea