第一张照片是我当前在excel中的输出,第二张照片显示了我想要的
我遇到了麻烦,我在数据集中的列标题名称下方插入了一个空行,我希望用拆分的列标题名称填充该空行。像(1-600,1-700,1-800是分裂的独立单元格 像所有的一样,这里是1年,而600周是相同的
这是我的代码:
private void button4_Click(object sender, System.EventArgs e)
{
GetDataSet();
}
private void GetDataSet()
{
try
{
SqlConnection conn = new SqlConnection(Connection);
string data = null;
//int i = 0;
//int j = 0;
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
SqlCommand cmd = new SqlCommand("select * from [CAT].[Tables].[LT1005ReportYAS]", conn);
cmd.Parameters.AddWithValue("@TransactionID", "2603_LT023_13022019174458");
cmd.Parameters.AddWithValue("@StartDate", "2019-01-01");
cmd.Parameters.AddWithValue("@EndDate", "2019-01-21");
cmd.Parameters.AddWithValue("@ClientID", 1030);
SqlDataAdapter da = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.CommandTimeout = 0;
da.SelectCommand = cmd;
DataSet ds = new DataSet();
conn.Open();
da.Fill(ds);
var dr = ds.Tables[0].NewRow();
ds.Tables[0].Rows.InsertAt(dr, 0);
ds.Tables[0].TableName = "Retailer";
ds.Tables[0].Columns.RemoveAt(0);
ds.Tables[0].Columns.RemoveAt(0);
Microsoft.Office.Interop.Excel.Application objexcelapp = new Microsoft.Office.Interop.Excel.Application();
objexcelapp.Application.Workbooks.Add(Type.Missing);
objexcelapp.Columns.AutoFit();
for (int i = 1; i < ds.Tables[0].Columns.Count + 1; i++)
{
Microsoft.Office.Interop.Excel.Range xlRange = (Microsoft.Office.Interop.Excel.Range)objexcelapp.Cells[1, i];
xlRange.Font.Bold = -1;
xlRange.Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
xlRange.Borders.Weight = 1d;
xlRange.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
objexcelapp.Cells[1, i] = ds.Tables[0].Columns[i - 1].ColumnName;
}
foreach (DataTable dt in ds.Tables)
{
for (int i1 = 0; i1 < dt.Columns.Count; i1++)
{
xlWorkSheet.Cells[1, i1 + 1] = dt.Columns[i1].ColumnName;
}
}
for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
int s = i + 1;
for (int j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
{
data = ds.Tables[0].Rows[i].ItemArray[j].ToString();
xlWorkSheet.Cells[s + 1, j + 1] = data;
xlWorkSheet.Columns.AutoFit();
Microsoft.Office.Interop.Excel.Range xlRange = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[i + 2, j + 1];
xlRange.Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
//xlRange.Font.Bold = -1;
xlRange.Borders.Weight = 1d;
xlRange.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;
}
}
xlWorkBook.SaveAs("Retailer.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
MessageBox.Show("Excel file created");
conn.Close();
}
catch (Exception ex)
{
}
}
答案 0 :(得分:0)
您可以使用string.Split()来执行您要的操作。例如:
string exampleValue = "1-600";
// This will split the string at the '-' (hyphen) character and produce an array with 2 values.
string[] splitString = exampleValue.Split('-');
Console.WriteLine(splitString[0]); // Expected output: 1.
Console.WriteLine(splitString[1]); // Expected output: 600.
有帮助吗?