Purtroppo, non esiste un metodo diretto. Non so esattamente perché, ma gli sviluppatori di EPPlus scelgono di rendere la proprietà ExcelTable.Address
solo pronta. Quindi la scelta più ovvia sarebbe quella di creare una nuova tabella, copiare tutte le proprietà (supponendo che tu sappia tutte quelle necessarie) e cancellare quella vecchia. Non molto ideale dato che potresti perdere qualcosa.
Ma ne vedo altri due: il prossimo esattamente carino, ma meglio la copia/cancella. Assumere una tabella in questo modo:
Opzione 1 si potrebbe sborsare il codice sorgente EPPlus e rendere la proprietà aveva bisogno di lettura/scrittura. Si potrebbe fare questo nel file ExcelTable.cs
modificando:
public ExcelAddressBase Address
{
get
{
return _address;
}
internal set //REMOVE internal KEYWORD
{
_address = value;
SetXmlNodeString("@ref",value.Address);
WriteAutoFilter(ShowTotal);
}
}
togliendo la parola chiave internal
. Ma ovviamente dovrai fare attenzione facendo così non rompere qualcos'altro lungo la strada. Con questo, si può fare qualcosa di simile:
var fileInfo = new FileInfo(@"c:\temp\Expand_Table.xlsx");
using (var pck = new ExcelPackage(fileInfo))
{
var workbook = pck.Workbook;
var worksheet = workbook.Worksheets.First();
//Added 11th data row assuming the table is from A1 to C11 (Header row + 10 data rows)
worksheet.Cells["A12"].Value = 10;
worksheet.Cells["B12"].Value = 100;
worksheet.Cells["C12"].Value = Path.GetRandomFileName();
var tbl = worksheet.Tables["TestTable1"];
var oldaddy = tbl.Address;
var newaddy = new ExcelAddressBase(oldaddy.Start.Row, oldaddy.Start.Column, oldaddy.End.Row + 1, oldaddy.End.Column);
tbl.Address = newaddy;
pck.Save();
}
Opzione 2 sarebbe l'opzione più sicura ma più "sporco" utilizzando una stringa di sostituire sul XML. Non possiamo presumere che ci sia solo un riferimento all'indirizzo perché, ad esempio, il filtro automatico potrebbe essere acceso. Ecco ciò che il XML di default sarà simile a quando si crea una tabella in Excel (notare i 2 riferimenti al indirizzo):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<table xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" id="3" name="TestTable1" displayName="TestTable1" ref="A1:C11" totalsRowShown="0">
<autoFilter ref="A1:C11" />
<tableColumns count="3">
<tableColumn id="1" name="Col1" />
<tableColumn id="2" name="Col2" />
<tableColumn id="3" name="Col3" />
</tableColumns>
<tableStyleInfo name="TableStyleMedium2" showFirstColumn="0" showLastColumn="0" showRowStripes="1" showColumnStripes="0" />
</table>
modo che possiamo fare una stringa sostituire in questo modo:
var fileInfo = new FileInfo(@"c:\temp\Expand_Table.xlsx");
using (var pck = new ExcelPackage(fileInfo))
{
var workbook = pck.Workbook;
var worksheet = workbook.Worksheets.First();
//Added 11th data row assuming the table is from A1 to C11 (Header row + 10 data rows)
worksheet.Cells["A12"].Value = 10;
worksheet.Cells["B12"].Value = 100;
worksheet.Cells["C12"].Value = Path.GetRandomFileName();
var tbl = worksheet.Tables["TestTable1"];
var oldaddy = tbl.Address;
var newaddy = new ExcelAddressBase(oldaddy.Start.Row, oldaddy.Start.Column, oldaddy.End.Row + 1, oldaddy.End.Column);
//Edit the raw XML by searching for all references to the old address
tbl.TableXml.InnerXml = tbl.TableXml.InnerXml.Replace(oldaddy.ToString(), newaddy.ToString());
pck.Save();
}
EPPlus è per l'esportazione in Excel, cosa vuoi realmente? –
Se sto capendo correttamente la tua domanda, la mia risposta qui sotto leggerà un file Excel in memoria e aggiungerà una riga, quindi sovrascriverà il file. –