日期:2014-05-18  浏览次数:20505 次

请问如何在Excel模板中的第三行才插入数据
我想固定Excel模板的第一第二行为一格式(字体颜色大小等)而第三行开始是另外一种格式,但用常规的插入数据时在Excel模板中的第三行的格式也跟第二行的格式相同了,这不是我想要的格式。

我用的代码简写如下:
//...
for (int i = 0; i < ds.Tables["data_table"].Rows.Count; i++)
{
string intostr = "insert into [Sheet1$] (F1,F2,F3,F4,F5)values("//.....
//...

OleDbCommand cm = new OleDbCommand(intostr, Excel_conn);
cm.ExecuteNonQuery();//插入数据
}


请问如何才能在第三行才开始插入数据?





------解决方案--------------------
HSSFRow row1=sheet1.CreateRow(0);
------解决方案--------------------
http://www.cnblogs.com/tonyqus/archive/2009/03/23/1419988.html
------解决方案--------------------
string sql = "insert into [sheet1$A9:F15](FieldName1,FieldName2,…) values('a',’b’,…)";
------解决方案--------------------
不是winform,webform下操作方式不同
而是你用的是oldb连接操作
另一种是直接操作文件。
------解决方案--------------------
另外,可以你可以插入第一行的数据就是列标题
或者把第一行update
------解决方案--------------------
我都是直接调用excel类来实现输出

Excel.Application excelApp = new Excel.ApplicationClass(); // Creates a new Excel Application
excelApp.Visible = false; // Makes Excel invisible to the user.

string workbookPath;
if (_shiftFlag == true)
workbookPath = Application.StartupPath + "\\ExcelFormat.xls";
else
workbookPath = Application.StartupPath + "\\ExcelFormat.xls";
Excel.Workbook excelWorkbook;
bool OpenOK = false;
try
{
excelWorkbook = excelApp.Workbooks.Open(workbookPath, 0,
true, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true,
false, 0, true, false, false);
OpenOK = true;
}
catch
{
MessageBox.Show("Cannot open excel file " + Application.StartupPath + "\\Format.xls for input");
excelWorkbook = null;
excelApp.Quit();
excelApp = null;
}
if (OpenOK == true)
{
Excel.Sheets excelSheets = excelWorkbook.Worksheets;

// The following gets Sheet for editing
string currentShee="sheet1";

Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);
// The following gets cell B4 for Date editing
Excel.Range Date = (Excel.Range)excelWorksheet.get_Range("B4", "B4");
Date.Value2 = DateTime.Now.ToString("MM/dd/yyyy");

}
用这种方法你也可以直接把整个DATATABLE输出到EXCEL里