I have this code using openxml sdk which generates table in PPT report.
This line is the reason for table style.
tableStyleId.Text = "{5C22544A-7EE6-4342-B048-85BDC9FD1C3A}";
Style is :
I need to change the style and colors but I couldn't find anything for that. Please help.
private D.Table GenerateTable(int projectID, string reportType)
{
// Declare and instantiate table
D.Table table = new D.Table();
// Specify the required table properties for the table
D.TableProperties tableProperties = new D.TableProperties() { FirstRow = true, BandRow = false };
D.TableStyleId tableStyleId = new D.TableStyleId();
tableStyleId.Text = "{5C22544A-7EE6-4342-B048-85BDC9FD1C3A}";
tableProperties.Append(tableStyleId);
D.TableGrid tableGrid1 = new D.TableGrid();
System.Data.DataTable dtData = new System.Data.DataTable();
if (reportType == "projcharter")
{
//tblXBenefit
dtData = GetBenefit(projectID);
// Declare and instantiate tablegrid and colums
//D.TableGrid tableGrid1 = new D.TableGrid();
D.GridColumn gridColumn1 = new D.GridColumn() { Width = 1848000L };
D.GridColumn gridColumn2 = new D.GridColumn() { Width = 648000L };
D.GridColumn gridColumn3 = new D.GridColumn() { Width = 648000L };
D.GridColumn gridColumn4 = new D.GridColumn() { Width = 648000L };
tableGrid1.Append(gridColumn1);
tableGrid1.Append(gridColumn2);
tableGrid1.Append(gridColumn3);
tableGrid1.Append(gridColumn4);
}
table.Append(tableProperties);
table.Append(tableGrid1);
// Instantiate the table header row
D.TableRow tableHRow = new D.TableRow() { Height = 0L };
for (int column = 0; column < dtData.Columns.Count; column++)
{
tableHRow.Append(CreateTextCell(dtData.Columns[column].ToString()));
}
table.Append(tableHRow);
// Instantiate the table data row
for (int row = 0; row < dtData.Rows.Count; row++)
{
// Instantiate the table row
D.TableRow tableRow = new D.TableRow() { Height = 0L };
for (int column = 0; column < dtData.Columns.Count; column++)
{
tableRow.Append(CreateTextCell(dtData.Rows[row][column].ToString()));
}
table.Append(tableRow);
}
return table;
}
Links to previous questions:
create dynamic table in powerpoint using openXML with c# and ASP.net
unable to generate second table in PPT report using openxml
How to ignore/solve "Repair" message from Powerpoint report generated by OpenXML with ASP.net
Solved it.
So what I did was, I created a new ppt > inserted tables with required design manually in it and then saved it as ppt xml (you will see this option in save as).
Open that xml search for tablestyleid and use that value in the code.
Related
I have this project on c# windows forms and what I have to do is get some data from a web service and present them to FastRaport.
I have tried display them on a DataGridView first and then fetching the data from it and it works pretty nice. I can download the file and then it gives me a dialog asking me in what format do I want to save it.
But now the requirement is to not use gridview at all, just a button that would convert directly those data into a PDF file.
Here's my code for displaying the data into DataGridView:
var asd1 = dataGridView1.Columns.Add("DrzavaAng", "DrzavaAng");
var asd2 = dataGridView1.Columns.Add("Valuta", "Valuta");
var asd3 = dataGridView1.Columns.Add("Oznaka", "Oznaka");
var asd4 = dataGridView1.Columns.Add("Nomin", "Nomin");
var asd5 = dataGridView1.Columns.Add("Sreden", "Sreden");
for (var i = 0; i<xmlNodes.Count; i++)
{
var node = xmlNodes[i];
var states = new Class();
states.Valuta = node["Valuta"].InnerText;
states.Oznaka = node["Oznaka"].InnerText;
states.Nomin = node["Nomin"].InnerText;
states.Sreden = node["Sreden"].InnerText;
states.DrzavaAng = node["DrzavaAng"].InnerText;
//var asd = dataGridView1.Columns.Add("1", "1");
_ = dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells["DrzavaAng"].Value=states.DrzavaAng;
dataGridView1.Rows[i].Cells["Valuta"].Value=states.Valuta;
dataGridView1.Rows[i].Cells["Oznaka"].Value=states.Oznaka;
dataGridView1.Rows[i].Cells["Nomin"].Value=states.Nomin;
dataGridView1.Rows[i].Cells["Sreden"].Value=states.Sreden;
}
Here's the part where I pass the data from GridView to the FastReport "database".
using (Report report = new Report())
{
report.Load(ReportPath);
DataTable dt = new DataTable();
foreach (DataGridViewColumn cl in dataGridView1.Columns)
{
dt.Columns.Add();
}
object[] clvl = new object[dataGridView1.Columns.Count];
foreach (DataGridViewRow row in dataGridView1.Rows)
{
for (int i = 0; i<row.Cells.Count; i++)
{ clvl[i] = row.Cells[i].Value; }
dt.Rows.Add(clvl);
}
DataSet ds = new DataSet();
ds.Tables.Add(dt);
report.Dictionary.RegisterData(ds.Tables[0], "test", true);
report.SetParameterValue("date", dateTimePicker1.Value.ToString("dd.MM.yyyy"));
report.Show();
}
Here's my FastReport file before committing anything:
FastReportImage
And here's the output I get : FastReport output
My question is, how can I make it to fetch the data without the gridview, directly from webservice and load it into a FastReport -> PDF file?
I'm trying to populate a combobox with the names of the columns in a spreadsheet.
I'm using the spreadsheetlight library. I can set the cell value using the following code where A refers to column name and 1 refers to row name. (Am I right?)
But how can I get the the name of all columns in all sheets?
SLDocument sl = new SLDocument();
sl.SetCellValue("A1", true);
First, get the last column index using SLWorksheetStatistics:
SLWorksheetStatistics stats = sl.GetWorksheetStatistics();
int endColumnIndex = stats.EndColumnIndex;
Then iterate through the columns:
var headers = new List<string>();
for (int i = 1; i <= endColumnIndex; i++){
headers.Add(sl.GetCellValueAsString(1, i));
}
The following will print the values "foo" and "bar" from the column list:
var fileName = "test.xlsx";
var sl = new SLDocument(fileName);
foreach (var sheetName in sl.GetWorksheetNames())
{
SLDocument sheet = new SLDocument(fileName, sheetName);
sheet.SetCellValue("A1", "foo");
sheet.SetCellValue("B1", "bar");
SLWorksheetStatistics stats = sheet.GetWorksheetStatistics();
int endColumnIndex = stats.EndColumnIndex;
var headers = new List<string>();
for (int i = 1; i <= endColumnIndex; i++)
{
headers.Add(sheet.GetCellValueAsString(1, i));
}
foreach (var column in headers)
{
Console.WriteLine(column);
}
Console.ReadKey();
}
Trying to style a table using a predefined style but nothing is working. I've tried with a a newly created document and one created from a saved template. Using the SDK Productivity tool I can see the style is there in the template but it's not being applied. I've tried appended the style or setting it directly and neither seem to work.
public static void CreateWordprocessingDocument(string fileName) {
string[,] data = {
{"Texas", "TX"},
{"California", "CA"},
{"New York", "NY"},
{"Massachusetts", "MA"}
};
using (var wordDocument = WordprocessingDocument.Open(fileName, true)) {
// We need to change the file type from template to document.
wordDocument.ChangeDocumentType(WordprocessingDocumentType.Document);
var body = wordDocument.GetDocument().Body;
Table table = new Table();
TableProperties props = new TableProperties();
TableStyle tableStyle = new TableStyle { Val = "Light Shading Accent 1" };
props.TableStyle = tableStyle;
//props.Append(tableStyle);
table.AppendChild(props);
for (var i = 0; i <= data.GetUpperBound(0); i++) {
var tr = new TableRow();
for (var j = 0; j <= data.GetUpperBound(1); j++) {
var tc = new TableCell();
tc.Append(new Paragraph(new Run(new Text(data[i, j]))));
tc.Append(new TableCellProperties(new TableCellWidth { Type = TableWidthUnitValues.Auto }));
tr.Append(tc);
}
table.Append(tr);
}
body.Append(table);
wordDocument.GetDocument().Save();
}
}
Finally figured it out. I was using the style name and not the style id. So the line where the style is declared should look like:
TableStyle tableStyle = new TableStyle { Val = "LightShading-Accent1" };
I am using dbAutoTrack for generate PDF in that I have to add Table but I get the Exception like object references is not set to an instance of object
Below is my code
Table t=new Table();
Row row=new Row(t);
row.Cells.Add("ABC");
t.Rows.Add(row);
_pdfGraphics.DrawTable(100,200, t);
where _pdfGraphics is object of PDFGraphics.
Thanks in Advances
Most probably you have message like: "Not enough columns in this row to have a column span of 1." And you needed just to add them.
Workable sample:
//Initialize a new PDF Document
Document _document = new Document();
_document.Title = "Аpitron Sample";
_document.Author = "StanlyF";
_document.Creator = "АPItron LTD.";
Page page = null;
PDFGraphics graphics = null;
Table _table = new Table();
_table.Columns.Add(30);
Row row = new Row(_table);
row.Height = 25;
row.Cells.Add("ABC");
_table.Rows.Add(row);
while (_table != null)
{
//Initialize new page with default PageSize A4
page = new Page(PageSize.A4);
//Add page to document
_document.Pages.Add(page);
//Get the PDFGraphics object for drawing to the page.
graphics = page.Graphics;
_table = graphics.DrawTable(100,200, _table);
}
using(FileStream _fs = new FileStream("Table_Sample.pdf", System.IO.FileMode.Create, System.IO.FileAccess.Write))
{
//Generate PDF to the stream
_document.Generate(_fs);
Process.Start("Table_Sample.pdf");
}
The accepted answer didn't work for me and I was still getting null reference exceptions. It turns out that the rows need to be added with a TableStyle otherwise the cells, for whatever reason, won't be generated:
StandardFonts standardfont = StandardFonts.Helvetica;
PDFFont fontSmall_reg = new PDFFont(standardfont, FontStyle.Regular);
TableStyle ts = new TableStyle(fontSmall_reg, 12, nullBorder);
Table data = new Table(ts);
data.Columns.Add(100);
data.Columns.Add(100);
Row r = new Row(data, ts);
r.Height = 25;
r.Cells.Add("Row 1 Col1");
data.add(r)
r.Cells.Add("Row 1 Col2");
I'm having problems with exporting my values in my DataGridView to PDF using FastReport.
I believe I have the correct code for it, but the rows in my PDF doesn't seem to be updating, it only displays the first row of my DataGridView.
private void CreateDataSet()
{
ds = new DataSet();
DataTable table = new DataTable();
table.TableName = "ABCD";
ds.Tables.Add(table);
{
for (int i = 0; i < dg.ColumnCount; i++)
{
table.Columns.Add(dg.Columns[i].HeaderText);
}
for (int j = 0; j < dg.Rows.Count; j++)
{
table.Rows.Add(dg.Rows[j].Cells[0].FormattedValue.ToString(), dg.Rows[j].Cells[1].FormattedValue.ToString(), dg.Rows[j].Cells[2].FormattedValue.ToString(), dg.Rows[j].Cells[0].FormattedValue.ToString(), dg.Rows[j].Cells[4].FormattedValue.ToString(), dg.Rows[j].Cells[5].FormattedValue.ToString());
}
}
}
I even tried removing the For Loop and replacing it with this:
table.Rows.Add(dg.Rows[0].Cells[0].FormattedValue.ToString(), dg.Rows[0].Cells[1].FormattedValue.ToString(), dg.Rows[0].Cells[2].FormattedValue.ToString(), dg.Rows[0].Cells[0].FormattedValue.ToString(), dg.Rows[0].Cells[4].FormattedValue.ToString(), dg.Rows[0].Cells[5].FormattedValue.ToString());
table.Rows.Add(dg.Rows[1].Cells[0].FormattedValue.ToString(), dg.Rows[1].Cells[1].FormattedValue.ToString(), dg.Rows[1].Cells[2].FormattedValue.ToString(), dg.Rows[1].Cells[0].FormattedValue.ToString(), dg.Rows[1].Cells[4].FormattedValue.ToString(), dg.Rows[1].Cells[5].FormattedValue.ToString());
But no avail.
Here's the output for the code above in PDF: http://i1296.photobucket.com/albums/ag2/paozaf/output_zpsde2b6278.png
Desired data output: http://i1296.photobucket.com/albums/ag2/paozaf/desiredoutput_zps44a27fac.png
The cause of your issue (it only displays the first row) is empty value of DataBand. You should assign data for it.
Report report = new Report;
report.Load("reportTemplate.frx");
report.RegisterData(dataSet);
report.GetDataSource("tableName").Enabled= true;
DataBand db = report.FindObject("DataBand1") as DataBand;
db.DataSource = report.GetDataSource("tableName");
example