<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ジャナ・ビジネス・コンサルティング &#187; OpenXML</title>
	<atom:link href="http://www.janabiz.com/tag/openxml/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.janabiz.com</link>
	<description>新聞、電子出版、Webシステム開発 SAP ERP開発 人材派遣 Java Android プログラマ学校</description>
	<lastBuildDate>Sat, 04 Mar 2023 03:22:06 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>OpenXML Spreadsheet, how to copy and insert specified rows</title>
		<link>http://www.janabiz.com/openxml-copy-spreadsheet-rows/</link>
		<comments>http://www.janabiz.com/openxml-copy-spreadsheet-rows/#comments</comments>
		<pubDate>Mon, 13 May 2013 09:44:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Excel]]></category>
		<category><![CDATA[Spreadsheet]]></category>
		<category><![CDATA[OpenXML]]></category>

		<guid isPermaLink="false">http://new.janabiz.com/?p=371</guid>
		<description><![CDATA[<p>We usually use OpenXML spreadsheet to create excel document and report. For some document and report, sometimes we hope copy and insert some rows or columns automatically. For example , we want to output a list data into excel worksheet whose element has 3 formatted rows including some merge cells etc. If list&#8217;s size is 10, we hope copy 3 formatted rows of first element 10 times and insert copied rows below the first element rows automatically. In OpenXML spreadsheet, when you copy some rows and insert copied rows into destination, there are many works to do. First, in the copied rows, maybe there are some merge cells, data validate &#8230; <a href="http://www.janabiz.com/openxml-copy-spreadsheet-rows/">Continue reading <span class="meta-nav">&#8594;</span></a></p><p>The post <a href="http://www.janabiz.com/openxml-copy-spreadsheet-rows/">OpenXML Spreadsheet, how to copy and insert specified rows</a> appeared first on <a href="http://www.janabiz.com">ジャナ・ビジネス・コンサルティング</a>.</p>]]></description>
				<content:encoded><![CDATA[<p>We usually use OpenXML spreadsheet to create excel document and report. For some document and report, sometimes we hope copy and insert some rows or columns automatically. For example , we want to output a list data into excel worksheet whose element has 3 formatted rows including some merge cells etc. If list&#8217;s size is 10, we hope copy 3 formatted rows of first element 10 times and insert copied rows below the first element rows automatically. </p>
<p>In OpenXML spreadsheet, when you copy some rows and insert copied rows into destination, there are many works to do. First, in the copied rows, maybe there are some merge cells, data validate list ( pull down cells), formulas, drawings, charts. You must change the row&#8217;s index number to the inserted rows. Then the row&#8217;s index number for objects below in the destination must be changed. These objects include normal cells, merge cells, data validate list, formulas, drawings, charts etc.</p>
<p>The following is part of code to explain how to implement copy and insert. About the complete examples, please <a href="/dl?pcode=200002" alt="OpenXML spreadsheet | Excel solution | Excel Report | Excelソリューション">Download ExcelTDF Package</a> </p>
<div class="div-example">
<pre style="margin: 0; line-height: 125%">        <span style="color: #008000">/// &lt;summary&gt;                                                      </span>
        <span style="color: #008000">/// Copy the part of rows from srcRowFrom to srcRowTo between srcColFrom and srcColTo. </span>
        <span style="color: #008000">/// The row index, whose  column is between column srcColFrom and srcColTo, and whose</span>
        <span style="color: #008000">/// row is below the destRowFrom, will be changed after cloned cells added into worksheet.</span>
        <span style="color: #008000">/// &lt;/summary&gt;</span>
        <span style="color: #008000">/// &lt;param name=&quot;workbookPart&quot;&gt;&lt;see cref=&quot;WorkbookPart&quot;/&gt;&lt;/param&gt;</span>
        <span style="color: #008000">/// &lt;param name=&quot;sheetIndex&quot;&gt;worksheet index&lt;/param&gt;</span>
        <span style="color: #008000">/// &lt;param name=&quot;srcRowFrom&quot;&gt;source from row index&lt;/param&gt;</span>
        <span style="color: #008000">/// &lt;param name=&quot;srcRowTo&quot;&gt;source to row index&lt;/param&gt;</span>
        <span style="color: #008000">/// &lt;param name=&quot;destRowFrom&quot;&gt;destination from row index&lt;/param&gt;</span>
        <span style="color: #008000">/// &lt;param name=&quot;copyTimes&quot;&gt;the times to be copied&lt;/param&gt;</span>
        <span style="color: #008000">/// &lt;param name=&quot;srcColFrom&quot;&gt;limitation of column left&lt;/param&gt;</span>
        <span style="color: #008000">/// &lt;param name=&quot;srcColTo&quot;&gt;limitation of column right&lt;/param&gt;</span>
        <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> CopyRowRange(WorkbookPart workbookPart, <span style="color: #2b91af">int</span> sheetIndex,
            <span style="color: #2b91af">int</span> srcRowFrom, <span style="color: #2b91af">int</span> srcRowTo, <span style="color: #2b91af">int</span> destRowFrom, <span style="color: #2b91af">int</span> copyTimes, <span style="color: #2b91af">int</span> srcColFrom, <span style="color: #2b91af">int</span> srcColTo)
        {
            <span style="color: #008000">//only support copy down </span>
            <span style="color: #0000ff">if</span> (copyTimes &lt;= 0 || srcRowTo &lt; srcRowFrom || destRowFrom &lt; srcRowFrom) <span style="color: #0000ff">return</span>;
            <span style="color: #2b91af">int</span> destRowFromBase = destRowFrom;

            <span style="color: #008000">//Get the source sheet to be copied</span>
            WorksheetPart worksheetPart = GetWorksheetPart(workbookPart, sheetIndex);
            SheetData sheetData = worksheetPart.Worksheet.GetFirstChild&lt;SheetData&gt;();

            <span style="color: #008000">//get cells to be cloned according to the specified rows and columns</span>
            IList&lt;Cell&gt; cells = sheetData.Descendants&lt;Cell&gt;().Where(c =&gt;
                GetRowIndex(c.CellReference) &gt;= srcRowFrom &amp;&amp;
                GetRowIndex(c.CellReference) &lt;= srcRowTo &amp;&amp;
                GetCellColIndex(c.CellReference) &gt;= srcColFrom &amp;&amp;
                GetCellColIndex(c.CellReference) &lt;= srcColTo).ToList&lt;Cell&gt;();

            <span style="color: #008000">//no cells to be cloned</span>
            <span style="color: #0000ff">if</span> (cells.Count() == 0) <span style="color: #0000ff">return</span>;


            <span style="color: #008000">//after cloned, the index of rows from destRowFrom should be changed</span>
            <span style="color: #008000">//diff rows between srcColFrom and srcColTo</span>
            <span style="color: #2b91af">int</span> copiedRowCount = srcRowTo - srcRowFrom + 1;

            <span style="color: #008000">//move row index between srcColFrom and srcColTo that the row index greater or equal to </span>
            <span style="color: #008000">//destRowFrom</span>
            MoveRowIndex(workbookPart, sheetIndex, destRowFrom - 1, copiedRowCount * copyTimes, srcColFrom, srcColTo);

            <span style="color: #008000">//temporary array of changed row index</span>
            IList&lt;<span style="color: #2b91af">int</span>&gt; changeRowIndexs = <span style="color: #0000ff">new</span> List&lt;<span style="color: #2b91af">int</span>&gt;();

            <span style="color: #008000">//cloned cells, row index/cells</span>
            IDictionary&lt;<span style="color: #2b91af">int</span>, IList&lt;Cell&gt;&gt; clonedCells = <span style="color: #0000ff">null</span>;

            <span style="color: #008000">//formula cells in cloned cells</span>
            IList&lt;Cell&gt; formulaCells = <span style="color: #0000ff">new</span> List&lt;Cell&gt;();

            <span style="color: #008000">//cloned related rows for speed</span>
            IList&lt;Row&gt; cloneRelatedRows = <span style="color: #0000ff">new</span> List&lt;Row&gt;();

            <span style="color: #0000ff">for</span> (<span style="color: #2b91af">int</span> i = 0; i &lt; copyTimes; i++)
            {
                <span style="color: #008000">//next copy col index</span>
                destRowFrom = destRowFromBase + copiedRowCount * i;
                <span style="color: #008000">// changed column index</span>
                <span style="color: #2b91af">int</span> changedRows = destRowFrom - srcRowFrom;

                <span style="color: #008000">//add change column index to list</span>
                changeRowIndexs.Add(changedRows);
                <span style="color: #008000">//clear formula cells</span>
                formulaCells.Clear();

                <span style="color: #008000">//1: cloned cells, row index/cell list </span>
                clonedCells = <span style="color: #0000ff">new</span> Dictionary&lt;<span style="color: #2b91af">int</span>, IList&lt;Cell&gt;&gt;();

                <span style="color: #0000ff">foreach</span> (Cell cell <span style="color: #0000ff">in</span> cells)
                {
                    Cell newCell = (Cell)cell.CloneNode(<span style="color: #0000ff">true</span>);
                    <span style="color: #2b91af">int</span>[] indexs = GetCellIndex(cell.CellReference);
                    <span style="color: #008000">//change row index of cloned cell</span>
                    <span style="color: #2b91af">int</span> rowIndex = indexs[1] + changedRows;
                    newCell.CellReference = GetColumnName(cell.CellReference) + rowIndex.ToString();

                    IList&lt;Cell&gt; rowCells = <span style="color: #0000ff">null</span>;
                    <span style="color: #0000ff">if</span> (clonedCells.ContainsKey(rowIndex))
                        rowCells = clonedCells[rowIndex];
                    <span style="color: #0000ff">else</span>
                    {
                        rowCells = <span style="color: #0000ff">new</span> List&lt;Cell&gt;();
                        clonedCells.Add(rowIndex, rowCells);
                    }
                    rowCells.Add(newCell);

                    <span style="color: #008000">//if is formula cell</span>
                    <span style="color: #0000ff">if</span> (newCell.CellFormula != <span style="color: #0000ff">null</span> &amp;&amp; newCell.CellFormula.Text.Length &gt; 0)
                    {
                        formulaCells.Add(newCell);
                    }
                }

                <span style="color: #008000">//add cloned cell into row</span>
                <span style="color: #0000ff">foreach</span> (<span style="color: #2b91af">int</span> rowIndex <span style="color: #0000ff">in</span> clonedCells.Keys)
                {
                    Row row = sheetData.Elements&lt;Row&gt;().Where(r =&gt; r.RowIndex == rowIndex).FirstOrDefault();
                    <span style="color: #008000">//if no this row </span>
                    <span style="color: #0000ff">if</span> (row == <span style="color: #0000ff">null</span>)
                    {
                        row = <span style="color: #0000ff">new</span> Row() { RowIndex = (<span style="color: #2b91af">uint</span>)rowIndex };
                        <span style="color: #008000">//find insert position</span>
                        Row refRow = sheetData.Elements&lt;Row&gt;().Where(r =&gt; r.RowIndex &gt; rowIndex).OrderBy(r =&gt; r.RowIndex).FirstOrDefault();
                        <span style="color: #0000ff">if</span> (refRow == <span style="color: #0000ff">null</span>)
                            sheetData.AppendChild&lt;Row&gt;(row);
                        <span style="color: #0000ff">else</span>
                            sheetData.InsertBefore&lt;Row&gt;(row, refRow);
                    }
                    row.Append(clonedCells[rowIndex].ToArray());

                    <span style="color: #008000">//add into clone related rows</span>
                    cloneRelatedRows.Add(row);
                }

                <span style="color: #008000">//2: processing copied range&#39;s formula </span>
                ChangeFormulaRowNumber(formulaCells, changedRows);

                <span style="color: #008000">//3: copy drawings in range</span>
                CopyDrawingsInRange(worksheetPart, srcRowFrom, srcRowTo, srcColFrom, srcColTo, destRowFrom, -1);
            }

            <span style="color: #008000"></span>
            <span style="color: #0000ff">foreach</span> (Row row <span style="color: #0000ff">in</span> cloneRelatedRows)
            {
                <span style="color: #008000">// sort by column name</span>
                IList&lt;Cell&gt; cs = row.Elements&lt;Cell&gt;().OrderBy(c =&gt; GetCellColIndex(c.CellReference.Value)).ToList&lt;Cell&gt;();
                row.RemoveAllChildren();
                row.Append(cs.ToArray());
            }
            
            <span style="color: #008000">//2: process merge cell in cloned rows</span>
            MergeCells mcells = worksheetPart.Worksheet.GetFirstChild&lt;MergeCells&gt;();
            <span style="color: #0000ff">if</span> (mcells != <span style="color: #0000ff">null</span>)
            {
                IList&lt;MergeCell&gt; newMergeCells = <span style="color: #0000ff">new</span> List&lt;MergeCell&gt;();
                IEnumerable&lt;MergeCell&gt; clonedMergeCells = mcells.Elements&lt;MergeCell&gt;().
                    Where(m =&gt; MergeCellInRange(m, srcRowFrom, srcRowTo, srcColFrom, srcColTo)).ToList&lt;MergeCell&gt;();
                <span style="color: #0000ff">foreach</span> (MergeCell cmCell <span style="color: #0000ff">in</span> clonedMergeCells)
                {
                    <span style="color: #0000ff">foreach</span> (<span style="color: #2b91af">int</span> changedRows <span style="color: #0000ff">in</span> changeRowIndexs)
                    {
                        MergeCell newMergeCell = CreateChangedRowMergeCell(cmCell, changedRows);
                        newMergeCells.Add(newMergeCell);
                    }
                }
                <span style="color: #2b91af">uint</span> count = mcells.Count.Value;
                mcells.Count = <span style="color: #0000ff">new</span> UInt32Value(count + (<span style="color: #2b91af">uint</span>)newMergeCells.Count);
                mcells.Append(newMergeCells.ToArray());
            }

            <span style="color: #008000">//3: process datavalidate list</span>
            IDictionary&lt;<span style="color: #2b91af">string</span>, DataValidation&gt; validates = GetDataValidatesInRange(worksheetPart, srcRowFrom, srcRowTo, srcColFrom, srcColTo);
            <span style="color: #0000ff">foreach</span> (<span style="color: #2b91af">string</span> cellname <span style="color: #0000ff">in</span> validates.Keys)
            {
                <span style="color: #0000ff">foreach</span> (<span style="color: #2b91af">int</span> changedRows <span style="color: #0000ff">in</span> changeRowIndexs)
                {
                    AddDataValidateRefItemOfChangedRow(cellname, changedRows, validates[cellname]);
                }
            }
        }
</pre>
</div>
<p>The post <a href="http://www.janabiz.com/openxml-copy-spreadsheet-rows/">OpenXML Spreadsheet, how to copy and insert specified rows</a> appeared first on <a href="http://www.janabiz.com">ジャナ・ビジネス・コンサルティング</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.janabiz.com/openxml-copy-spreadsheet-rows/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

 Served from: www.janabiz.com @ 2026-05-02 03:55:55 by W3 Total Cache -->