How to create a new page on each new table?
It means that the table should be placed on one page.
The following code is a sample code that outputs login, but does not create a new page for each table.
List<System.Data.DataTable> bbb = new List<System.Data.DataTable>();
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.Add("Columns 1");
dt.Columns.Add("Columns 2");
dt.Columns.Add("Columns 3");
dt.Rows.Add("aaa", "aaa", "aaa");
dt.Rows.Add("bbb", "bbb", "bbb");
dt.Rows.Add("ccc", "ccc", "ccc");
System.Data.DataTable dt2 = new System.Data.DataTable();
dt2.Columns.Add("Columns 5");
dt2.Rows.Add("aaa5");
bbb.Add(dt);
bbb.Add(dt2);
List<System.Data.DataTable> ListDataTable = new List<System.Data.DataTable>();
ListDataTable = bbb;
object objEndOfDoc = "\\endofdoc";
Microsoft.Office.Interop.Word.Document Wdc = new Microsoft.Office.Interop.Word.Document();
Microsoft.Office.Interop.Word.Range WordRange = Wdc.Bookmarks.get_Item(ref objEndOfDoc).Range;
Microsoft.Office.Interop.Word.Table wordTable;
for (int ListDataT = 0; ListDataT < ListDataTable.Count; ListDataT++)
{
int iRowCount = ListDataTable[ListDataT].Rows.Count;
int iColCount = ListDataTable[ListDataT].Columns.Count;
object objMissing = System.Reflection.Missing.Value;
wordTable = Wdc.Tables.Add(WordRange, iRowCount, iColCount, ref objMissing, ref objMissing);
int iTableRow = 1;
int iTableCol = 1;
for (int i = 0; i < ListDataTable[ListDataT].Columns.Count; i++)
{
wordTable.Cell(iTableRow, iTableCol).Range.Text = ListDataTable[ListDataT].Columns[i].ColumnName;
iTableCol++;
}
iTableRow++;
for (int i = 0; i < ListDataTable[ListDataT].Rows.Count; i++)
{
iTableCol = 1;
for (int j = 0; j < ListDataTable[ListDataT].Columns.Count; j++)
{
wordTable.Cell(iTableRow, iTableCol).Range.Text = ListDataTable[ListDataT].Rows[i][j].ToString();
// Console.Write(dt.Rows[i][j].ToString());
iTableCol++;
}
iTableRow++;
}
wordTable.Borders.Enable = 1;
wordTable.set_Style("Light Grid - Accent 3");
}
Wdc.SaveAs("c://test.docx");
Wdc.Close();
There are two possibilities:
1) Format the first row of the table with the paragraph formatting "Page Break Before".
2) Insert a manual Page Break between each table (keyboard equivalent: Shift+Enter).
Being a Word professional, my inclination is to use (1) unless something speaks against it. Most people, however, tend to use (2) because it's more obvious/discoverable.
wordTable.Rows[1].Range.ParagraphFormat.PageBreakBefore = true;
OR
Word.Range rng = wordTable.Range;
rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
rng.InsertBreak(Word.WdBreakType.wdPageBreak);
Related
I want to make data entry grid with validations and item template field which consist of multiple Webform controls. like SAP and Ax Dynamics data entry grid.
Kindly help me if you anyone have this kind of sample grid.
//You can store a DataTable in the session state
DataTable table = Session["Table1"] as DataTable;
table = new DataTable();
DataColumn colid = table.Columns.Add("sno", typeof(Int32));
DataColumn l_ID = table.Columns.Add("l_ID", typeof(String));
table.PrimaryKey = new DataColumn[] { colid };
colid.ReadOnly = true;
l_ID.ReadOnly = true;
getlist();
//List<Dictionary<string, string>> list = grid.GRCol();
for (int i = 0; i < list.Count; i++)
{
DataColumn dcol = new DataColumn(list[i]["fieldid"], typeof(string));
table.Columns.Add(dcol);
}
#region rowgenrate
for (int i = 1; i <= 1; i++)
{
DataRow aRow = table.NewRow();
for (int j = 0; j < table.Columns.Count; j++)
{
if (i == 1 && j > 1)
{
string abc = table.Columns[j].ToString();
aRow["sno"] = i;
aRow["l_ID"] = "001";
aRow[abc] = "";
//table.Rows.Add(aRow);
}
else
{
if (j > 1)
{
aRow["sno"] = i;
aRow["l_ID"] = "";
aRow[j] = "";
}`enter code here`
//table.Rows.Add(aRow);
}
}
table.Rows.Add(aRow);
}
#endregion
Session["Table1"] = table;
I am trying to create a word table from xml file but the rows are not in their proper order,ie the first element occurs as last row, 2nd as 1st row and so on. Where am i going wrong?
Word.Table oTable;
Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
oTable = oDoc.Tables.Add(wrdRng, ds.Tables[0].Rows.Count, ds.Tables[0].Columns.Count, ref oMissing, ref oMissing);
oTable.Range.ParagraphFormat.SpaceAfter = 6;
oTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
oTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
for (int r = 1; r <= ds.Tables[0].Rows.Count; r++)
{
for (int c = 1; c <= ds.Tables[0].Columns.Count; c++)
{
oTable.Cell(r, c).Range.Text = ds.Tables[0].Rows[r-1].ItemArray[c-1].ToString();
}
}
Here is the sample code to create simple word file with Table. It will give you a loop hint how to proceed with it.
class Program
{
static void Main(string[] args)
{
Document doc = new Document();
Section s = doc.AddSection();
Table table = s.AddTable(true);
//Create Header and Data
String[] header = { "Item", "Description", "Qty", "Unit Price", "Price" };
String[][] data = {
new String[]{ "Spire.Doc for .NET",".NET Word Component","1","$799.00","$799.00"},
new String[]{"Spire.XLS for .NET",".NET Excel Component","2","$799.00","$1,598.00"},
new String[]{"Spire.Office for .NET",".NET Office Component","1","$1,899.00","$1,899.00"},
new String[]{"Spire.PDF for .NET",".NET PDFComponent","2","$599.00","$1,198.00"},
};
//Add Cells
table.ResetCells(data.Length + 1, header.Length);
//Header Row
TableRow fRow = table.Rows[0];
fRow.IsHeader = true;
//Row Height
fRow.Height = 23;
//Header Format
fRow.RowFormat.BackColor = Color.AliceBlue;
for (int i = 0; i < header.Length; i++)
{
//Cell Alignment
Paragraph p = fRow.Cells[i].AddParagraph();
fRow.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
p.Format.HorizontalAlignment = HorizontalAlignment.Center;
//Data Format
TextRange TR = p.AppendText(header[i]);
TR.CharacterFormat.FontName = "Calibri";
TR.CharacterFormat.FontSize = 14;
TR.CharacterFormat.TextColor = Color.Teal;
TR.CharacterFormat.Bold = true;
}
//Data Row
for (int r = 0; r < data.Length; r++)
{
TableRow dataRow = table.Rows[r + 1];
//Row Height
dataRow.Height = 20;
//C Represents Column.
for (int c = 0; c < data[r].Length; c++)
{
//Cell Alignment
dataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
//Fill Data in Rows
Paragraph p2 = dataRow.Cells[c].AddParagraph();
TextRange TR2 = p2.AppendText(data[r][c]);
//Format Cells
p2.Format.HorizontalAlignment = HorizontalAlignment.Center;
TR2.CharacterFormat.FontName = "Calibri";
TR2.CharacterFormat.FontSize = 12;
TR2.CharacterFormat.TextColor = Color.Brown;
}
}
//Save and Launch
doc.SaveToFile("WordTable.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("WordTable.docx");
Console.ReadLine();
}
string[] SName = Request.Form.GetValues("Description");
string[] Email = Request.Form.GetValues("Email");
DataTable dtable = dt();
for (int i = 0; i <= SName.Length - 1; i++)
{
DataRow row1 = dtable.NewRow();
row1["Description"] = SName[i];
row1["Email"] = Email[i];
DAL.DMSS insertdata = new DMSS();
insertdata.INSERT_DATA(loggeduser.SUBSIDIARY_CD, input, SName[i], Email[i]);
}
above are my code to get the data from dynamic row add.
if i have 2 rows,data i get is :
now i want to add 1 more data ,sequence number
tried this code but not working..
for (int i = 0; i <= SName.Length - 1; i++)
{
if (i.length <2 )
{
string strvalue = i.PadLeft(2, '0');
}
else
{
string strvalue = i;
}
DataRow row1 = dtable.NewRow();
row1["Description"] = SName[i];
row1["Email"] = Email[i];
DAL.DMSS insertdata = new DMSS();
insertdata.INSERT_DATA(loggeduser.SUBSIDIARY_CD, input, SName[i], Email[i], strvalue);
}
for (int i = 0; i <= SName.Length - 1; i++)
{
var rowNumber = (i + 1).ToString("0#");
}
I have a piece of code below - which is looping through an Excel workbook adding the data to an SQL Database - However the forloop doesn't stop when the data runs out.
int rowCount = worksheet.Rows.Count -1;
for (int i = 3; i < rowCount; i++ )
{
Spreadsheet spreadsheetToSave = new Spreadsheet();
Estimate estimateToSave = new Estimate();
RAA raaToSave = new RAA();
spreadsheetToSave.Phases = worksheet.Cells[4, i].Value;
spreadsheetToSave.Deliverables = worksheet.Cells[5, i].Value;
if (worksheet.Cells[6,i].Value == "Y")
{
spreadsheetToSave.Scope = true;
}
else
{
spreadsheetToSave.Scope = false;
}
spreadsheetToSave.Description = worksheet.Cells[7, i].Value;
//estimateToSave.Estimate1 = Convert.ToDecimal(worksheet.Cells[8, i].Value);
//estimateToSave.Estimate2 = Convert.ToDecimal(worksheet.Cells[9, i].Value);
//estimateToSave.Estimate3 = Convert.ToDecimal(worksheet.Cells[10, i].Value);
estimateToSave.Estimate1 = 1;
estimateToSave.Estimate2 = 1;
estimateToSave.Estimate3 = 1;
spreadsheetToSave.Estimate = estimateToSave;
db.Spreadsheet.Add(spreadsheetToSave);
Can anyone help?
Thanks :)
Try replacing your first two lines of code with this:
int rowCount = worksheet.UsedRange.Rows.Count;
for (int i = 3; i <= rowCount; i++)
This loops from row 3 to the last filled row.
Also you should check the rest of your code: as far as is know in
worksheet.Cells[rowIndex, columnIndex]
the rowIndex comes first :)
I want to dynamically create a excel-like table with datagridview, but I want to have the option to set colspan to some columns.
The idea is just to display data, the user will not type anything, but it should be in table/spreadsheet look. If I cannot have datagridview with colspan is there any other type of table-like tool which has a colspan?
I other hand the columns will be created dynamically from database query result.
I'm using windows forms.
Any ideas?
You might take a look at the TableLayoutPanel Class, which has a TableLayoutPanel.SetColumnSpan Method.
Here's a code sample, which spans the one of the text boxes on 2 columns:
var dt = new DataTable();
dt.Columns.Add("Id");
dt.Columns.Add("Value1");
dt.Columns.Add("Value2");
dt.Rows.Add(1, "aa", "xx");
dt.Rows.Add(2, "bb","yy");
dt.Rows.Add(3, "cc", "zz");
tableLayoutPanel1.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
tableLayoutPanel1.ColumnCount = 4;
tableLayoutPanel1.AutoSize = true;
for (int i = 0; i < dt.Columns.Count; i++)
{
var l = new Label();
l.Dock = DockStyle.Fill;
l.Text = dt.Columns[i].ColumnName;
tableLayoutPanel1.Controls.Add(l, i, 0);
}
var emptyLabel = new Label();
emptyLabel.Text = "Empty label";
tableLayoutPanel1.Controls.Add(emptyLabel, 4, 0);
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Rows[i].ItemArray.Length; j++)
{
var tb = new TextBox();
tb.Multiline = true;
tb.Dock = DockStyle.Fill;
tb.Text = dt.Rows[i][j].ToString();
tableLayoutPanel1.Controls.Add(tb, j, i+1);
if (i == 1 && j == 2)
tableLayoutPanel1.SetColumnSpan(tb, 2);
}
}